An encounter with DB2 – How to apply “limit” to queries?

My new assignment wanted me to experiment with DB2. And like everyone else who is new to DB2, I couldn’t find a way to limit the result set! After a lot of googling and phone calls I ended up with a stupid solution – but it works.

The solution looks like this:

SELECT * FROM
	( SELECT ROW_NUMBER() OVER (ORDER BY column_to_be_sorted) AS rownumber, col1, col2, other_cols
	  FROM my_db2_table WHERE col1 = ‘something’ )
AS tmp_table
WHERE rownumber BETWEEN start_index AND end_index

I hope this helps. But I really wish if there was some better ways.

Advertisements