Friday, May 30, 2008

Incorrect Rownum in Oracle

The following query gives incorrect Rownum. The expectation is to count the rows after ordering has been done. But this query counts before ordering.

SELECT ROWNUM rowno2, a.*, pageBase FROM ramesh.EMPLOYEE a, (
SELECT (rowno - MOD(rowno,5)) AS pageBase FROM (
SELECT ROWNUM rowno, ID FROM (
SELECT JOINED_DATE, ID FROM ramesh.EMPLOYEE
) WHERE (JOINED_DATE > TO_DATE('2003-04-12' , 'yyyy-mm-dd')) ORDER BY ID DESC
) WHERE (ID = '17')
) WHERE (JOINED_DATE > TO_DATE('2003-04-12' , 'yyyy-mm-dd')) ORDER BY ID DESC


ROWNO2 ID FIRST_NAME LAST_NAME JOINED_DATE DEPT ROLE PAGEBASE
18 29 FN29 LN29 2003-04-29 00:00:00.0 DEP29 ROLE29 10
17 28 FN28 LN28 2003-04-28 00:00:00.0 DEP28 ROLE28 10
16 27 FN27 LN27 2003-04-27 00:00:00.0 DEP27 ROLE27 10
15 26 FN26 LN26 2003-04-26 00:00:00.0 DEP26 ROLE26 10
14 25 FN25 LN25 2003-04-25 00:00:00.0 DEP25 ROLE25 10
13 24 FN24 LN24 2003-04-24 00:00:00.0 DEP24 ROLE24 10
12 23 FN23 LN23 2003-04-23 00:00:00.0 DEP23 ROLE23 10
11 22 FN22 LN22 2003-04-22 00:00:00.0 DEP22 ROLE22 10
10 21 FN21 LN21 2003-04-21 00:00:00.0 DEP21 ROLE21 10
9 20 FN20 LN20 2003-04-20 00:00:00.0 DEP20 ROLE20 10
8 19 FN19 LN19 2003-04-19 00:00:00.0 DEP19 ROLE19 10
7 18 FN18 LN18 2003-04-18 00:00:00.0 DEP18 ROLE18 10
6 17 FN17 LN17 2003-04-17 00:00:00.0 DEP17 ROLE17 10
5 16 FN16 LN16 2003-04-16 00:00:00.0 DEP16 ROLE16 10
4 15 FN15 LN15 2003-04-15 00:00:00.0 DEP15 ROLE15 10
3 14 FN14 LN14 2003-04-14 00:00:00.0 DEP14 ROLE14 10
2 13 FN13 LN13 2003-04-13 00:00:00.0 DEP13 ROLE13 10
1 4 FN4 LN4 2004-01-20 00:00:00.0 DEP4 ROLE4 10


Get back to Developer Digest

No comments: