I am only a baby-geek when it comes to IBM--I am a SQL Server guy. But I found the rownumber approach (which I have used successfully in Oracle) did not work in DB2. I used this one:
SELECT
MYFIELD
FROM
"SCHEMANAME"."TABLENAME"
WHERE
FILTERCOL1 = 000001
AND FILTERCOL2 = 1
ORDER BY
MYFIELD DESC FETCH FIRST ROW ONLY
(I ordered descending because I needed the last value.)
I am only a baby-geek when it comes to IBM--I am a SQL Server guy. But I found the rownumber approach (which I have used successfully in Oracle) did not work in DB2. I used this one:
(I ordered descending because I needed the last value.)
Hope this helps. Joey
Remember to set an
ORDER BY
clause, because DB2 does not guarantee that the rows returned byFETCH FIRST N ROW ONLY
are always the same N.Strictly, there is no equivalent of TOP N in DB2.
compiles and runs, but
will not compile.
TOP N
andFETCH FIRST N
are not the same. You can only useFETCH FIRST
once per query, whereasTOP N
can be used in any sub-select.You can use a window function in a sub-query in order to simulate
TOP N
:This will return exactly 99 rows. I tried that in iSeries 7 and it worked.
Wouldn't it be easier to limit the result instead? Bellow is in order by date and I take the top result