| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> comp.databases.theory -> Re: auto increment in select clause
"Bob Badour" <bbadour_at_golden.net> wrote in message news:<Cq5M8.25136$Ey5.201345111_at_radon.golden.net>...
> "Samuel Tribehou" <s.tribehou_at_wanadoo.fr> wrote
> right
> > I should warn you that the above does not work correctly when there might be > duplicate 'ref' values. In general, one should do the '<' comparison on the > ranking attribute and the '=' comparison on a candidate key. > > One can turn the above into a quota query (ie. top N items) by adding the > appropriate HAVING clause.
Indeed:
sql>select count(t2.deptno) inc, t1.deptno as ref
2 from emp t1, emp t2
3 where t2.deptno <= t1.deptno
4 group by t1.deptno;
INC REF
---------- ----------
84 30
40 20
9 10
However:
sql>select count(distinct t2.deptno) inc, t1.deptno as ref
2 from emp t1, emp t2
3 where t2.deptno <= t1.deptno
4 group by t1.deptno;
INC REF
---------- ----------
3 30
2 20
1 10
Duplicates handling needs a PK:
sqlr>select
2 (select count(1) from emp ee where ee.empno <= e.empno) inc 3 , deptno
INC DEPTNO
---------- ----------
1 20
2 30
3 30
4 20
5 30
6 30
7 10
8 20
9 10
10 30
11 20
12 30
13 20
14 10
Received on Wed Jun 12 2002 - 22:33:50 CDT
![]() |
![]() |