This is just a part of a query, the actual query has a join and some other stuffs.
SQLWKS> select work_id, work_level from work
2> where work_level in(0,1)
3> order by work_level asc
4>
WORK_ID WORK_LEVEL
---------- ----------
737 0
734 0
826 0
1466 0
1544 0
2667 0
2220 0
1543 0
1234 0
1 1
2 1
What I want to do is I want to select work_level in certain order and do not want to get rest of the records.
The order is say work_level = 1, 0, 2, 5, 7, 9 (which is fixed but not serial) so I cannot use GREATEST or LEAST functions.
In the above example it should display only records with work_level = 1 and not =0.
Suppose there are no records with work_level = 1 then should go for work_level = 0 , if not then go for work_level = 2 if not then go for work_level = 5.
So it is a OR clause but with specific order and the processing should stop after it finds the match for the 1 st in the sequence.
Does any one know a query which will do this ??
I am not sure but if you were to use the decode like this:
select work_id, work_level from work
where work_level = DECODE(work_level,1,1,DECODE(work_level,0,0,DECODE(work_level,2,2,DECODE(
work_level,5,5,DECODE(work_level,7,7,9)))));
This is probably not efficient, but it gets the results you want.
select work_id, work_level from work
where work_level = 1
UNION
select work_id, work_level from work
where work_level = 0
UNION
select work_id, work_level from work
where work_level = 2
UNION
select work_id, work_level from work
where work_level = 5
UNION
select work_id, work_level from work
where work_level = 7
UNION
select work_id, work_level from work
where work_level = 9;
This will probably get you what you want (provided you are using 8i):
SELECT work_id, work_level FROM work
WHERE work_level =
(SELECT work_level FROM
(SELECT work_level FROM work
ORDER BY DECODE(work_id, 1,1, 0,2, 2,3, 5,4, 7,5, 9,6)
);
HTH,
Jurij Modic ASCII a stupid question, get a stupid ANSI
24 hours in a day .... 24 beer in a case .... coincidence?
Bookmarks