-
Fetch Records
Dear All,
Is it possible if I want to fetch record like below
From EMP Table there are deptno 10,11,12,13....20
I want to fetch First Dept like 13 then sorted ascending order
like
Deptno
13
10
11
12
14
15
16
17
18
19
20
Is it possible?
Thanks in Advance.
Saswata
-
Re: Fetch Records
[QUOTE=dsaswata;28283]Dear All,
Is it possible if I want to fetch record like below
From EMP Table there are deptno 10,11,12,13....20
I want to fetch First Dept like 13 then sorted ascending order
like
Deptno
13
10
11
12
14
15
16
17
18
19
20
Is it possible?
Thanks in Advance.
Saswata[/QUOTE]
Open two cursors.
In first cursor where clause select records related to deptno 13 only .
In Second cursor where clause specify the condition deptno <> 13.
-
Re: Fetch Records
select distinct deptno from emp where deptno = 13
union all
select distinct deptno from emp where deptno<>13;
-
Re: Fetch Records
First fetch the the desired first record .
then the other cecords and use order by in the second.
combine the both using Union.
-
Re: Fetch Records
hey there,
Well the answer given above is the good answer i mean that will help u out
select deptno from employee where deptno=13
union
select deptno from employee where deptno <>13
If the order by clause will be used alongwith i think it will message for an error.
Moreover by default the records fetched are in sorted manner.
all the best
-
Re: Fetch Records
I don't think that will throw an error if ORDER BY is added to the second part of the query at the end.
-
Re: Fetch Records
You can use order by clause in the second
select deptno from emp where deptno = 20
union
select deptno from emp where deptno <> 20 order by deptno;
but what is the use?
whether you specify order by or not , union first sorts records, then merge the records and finally eliminates duplicate records.
Using union you can't display output like this
20
10
30
use union all.
Further here two queries return mutually exclusive data. In such cases it is always advisable to use union all which gives better performance than union.