IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Peculiar query


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Guest







PostPosted: Sat Dec 10, 2005 8:45 am
Reply with quote

Dear buddy,

this is a peculiar db2 query I was asked in one interview.
___________________________________________________________
there is a employee table having three columns. those columns are

NAME | SALARY | FLAG
aa..........20.............y
bb..........45............n
xs..........23............y
we.........34.............n
df..........11.............y
tv..........20.............y
a flag is simply 'Y' if salary is paid, and 'N' if not paid.
___________________________________________________________
Then my question is, unfortunately the person who updated the table made wrong.
He should set all 'y's inplace of all 'n's and Vise versa
How to make this correct with a single query.
___________________________________________________________
my table should be like this.
NAME | SALARY | FLAG
aa..........20.............n
bb..........45............y
xs..........23............n
we.........34.............y
df..........11.............n
tv..........20.............n
Back to top
andy6703

New User


Joined: 25 Jul 2005
Posts: 14

PostPosted: Sat Dec 10, 2005 11:43 am
Reply with quote

Hey buddy,

One solution is when u retrieve the table the values will be as per your need which is shown below:

SELECT NAME, SALARY, FLAG,
CASE FLAG
WHEN 'Y' THEN 'N'
ELSE 'Y'
END
FROM EMPLOYEE

This query may not update the table but while retrieving values will meet your current requirements.

Hope this helps.

God Bless!
Back to top
View user's profile Send private message
Jag

New User


Joined: 07 Dec 2005
Posts: 30
Location: India

PostPosted: Sun Dec 11, 2005 11:58 am
Reply with quote

you have to follow three steps to get the work done..

1) First write a select query to get the employee names whose salary falg is "N"

Select empname
from emptable
where salaryfalg='N';


2)Update the table to set the salary flag to "N" whose flag is "y" earlier

Update emptable
set salaryfalg="N"
where salaryfalg="y";

This will set the salary flag to "n" whose flg is "y" before.By the query half work is done Right?

3) NOW set the salary falg of the employess (retrieved from 1 step) to "y"

update emptable
set salaryfalg='Y'
where empname="......(empnames fom first step)"


This will complete your job..But if anyone know better small process suggest the person who asked this ? as well as me too tyo improve my skills :-)

Cheers,
Jag.
Back to top
View user's profile Send private message
Guest







PostPosted: Tue Dec 13, 2005 12:17 am
Reply with quote

to andy.

HI buddy thanks for the reply. For selecting it is okay. But how to update?

to jag.

hey thanks for the reply. Imagine yourself when there are millions of records.
Back to top
EnjoyMF

New User


Joined: 27 May 2005
Posts: 88

PostPosted: Tue Dec 13, 2005 11:59 am
Reply with quote

Hi Guys

I suppose we can also use the below query for this particular query, this solves our purpose in a single query,
this can be used in a program or spufi or QMF

update employee
set FLAG =
case
when FLAG = 'y' then 'n'
when area = 'n' then 'y'
end
;


Cheers
Prasad
Back to top
View user's profile Send private message
Guest







PostPosted: Tue Dec 13, 2005 10:53 pm
Reply with quote

hi MR. enjoy,

hope that u r answer will solve the purpose.

Thanks
tvmkishore icon_lol.gif
Back to top
bhaskarbale

New User


Joined: 13 Dec 2005
Posts: 1

PostPosted: Wed Dec 14, 2005 11:04 am
Reply with quote

i want to know quary that will give second highest salary from emp table
Back to top
View user's profile Send private message
khamarutheen

Active Member


Joined: 23 Aug 2005
Posts: 677
Location: NJ

PostPosted: Wed Dec 14, 2005 11:58 am
Reply with quote

Hi frnd,

SELECT MAX(SALARY) FROM EMP WHERE SALARY<(SELECT MAX(SALARY) FROM EMP);

Hope it works
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
No new posts Issue with EXEC CICS QUERY SECURITY c... CICS 6
Search our Forums:

Back to Top