473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

ROWNUM functionality DB2 Ver 5

Hi Everbody

i am using db2 version 5. i have a requirement of querying based on row numbers.

for instance i might need to write a query, which looks like this

select * from employee_table fetch rows between 30 and 50

can anyone help me on how to achieve this in db2 ver 5
Mar 18 '08 #1
3 4604
docdiesel
297 Expert 100+
Hi,

I don't know if the function row_number() is supported in DB2 v5 (I suspect it's not but hope I'm wrong), but you could try the following:

Expand|Select|Wrap|Line Numbers
  1. Select
  2.   ROW_NUMBER() OVER(order by some_field asc) as ROWNO,
  3.   some,other,fields
  4. From
  5.   employee_table
  6. WHERE
  7.   ROWNO>30
  8. ANND
  9.   ROWNO<=50
  10. ;
Please let us know if DB2 v5 is able to work like this.

Regards,

Bernd
Mar 18 '08 #2
Hi Bernd

Thanks for you suggestion. But unfortunately for us, V5.0 doesnt support ROW_NUMBER() :(

Any more suggestions will be greatly appreciated.

Thanks

Raju


Hi,

I don't know if the function row_number() is supported in DB2 v5 (I suspect it's not but hope I'm wrong), but you could try the following:

Expand|Select|Wrap|Line Numbers
  1. Select
  2.   ROW_NUMBER() OVER(order by some_field asc) as ROWNO,
  3.   some,other,fields
  4. From
  5.   employee_table
  6. WHERE
  7.   ROWNO>30
  8. ANND
  9.   ROWNO<=50
  10. ;
Please let us know if DB2 v5 is able to work like this.

Regards,

Bernd
Mar 19 '08 #3
docdiesel
297 Expert 100+
Hi,

hm, then the only thing that's left to help you is FETCH FIRST x ROWS ONLY. But for that you'll need some order criteria, like an employee id, which I call "e_id" for the following example. You'll have to
  • select all rows e_id, ordered ascending by e_id
  • fetch the first 30 rows of them
  • and get the maximum value of e_id (means row 30)
  • then do the same for the 50th row
  • and finally use these ids to select * the appropriate rows
The code will look like this monster sql code:
Expand|Select|Wrap|Line Numbers
  1. select
  2.   *
  3. from
  4.   employee
  5. where
  6. -- e_id>30
  7.   e_id>(
  8.     select
  9.       max(tmptbl1.e_id)
  10.     from
  11.       (
  12.         select
  13.           e_id
  14.         from
  15.           employee
  16.         order by
  17.           e_id asc
  18.         fetch first 30 rows only
  19.       ) tmptbl1
  20.   )
  21.  
  22. and
  23. -- e_id<=50
  24.   e_id<=(
  25.     select
  26.       max(tmptbl2.e_id)
  27.     from
  28.       (
  29.         select
  30.           e_id
  31.         from
  32.           employee
  33.         order by
  34.           e_id asc
  35.         fetch first 50 rows only
  36.       ) tmptbl2
  37.   )
  38.  
  39. order by e_id asc
  40. ;
  41.  
and maybe it's awfully slow if your table is more than 10000 rows, but it's working on my test table.

Let me know if this fits to the needs of a v5 DB2.

Regards,

Bernd
Mar 19 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Paul Bradford | last post by:
I have a query that takes about two minutes and returns 97 rows. If I change the query only by adding ROWNUM to the outermost select clause, the query never returns (I let it run overnight). I'm...
9
by: Sandy | last post by:
I am trying to do the following: EXEC SQL UPDATE MY TABLE SET COL1 = :newValue WHERE COL1 = 0 AND ROWNUM = 1 ORDER BY COL2; (index on COL1, COL2)
4
by: pierig.gueguen | last post by:
Hello, I would like to know if the equivalent Oracle rownum exist in SQLServer. Here is a sample SQL code to explain what I want to do : select jobs.name, jobs.job_id, jobs.description,...
0
by: Jeff Boes | last post by:
I hope this helps someone else ... I had struggled some time ago with attempts to get a rank of values query to work, but then I gave up and set it aside. I had another reason to attack it, and in...
3
by: Reshmi Jacob | last post by:
Hi all I would like to know the order in which the records are saved in a table. I am a beginner in SQL, in Oracle we have the ROWNUM to retrive the same. Any option for this in SQL Server??? ...
2
by: krishhhna | last post by:
If we want to retrive 5,6,7 rows from a table,then we used rownum in a query in group by clause,but my doubt is rownum has individual values for each rows then how did you group rows based on...
1
by: joshua.h.song | last post by:
hi all. i need to convert the following oracle-based sql to DB2. SELECT rownum, name, birthday FROM person WHERE rownum < 10 could someone convert for me?
6
by: pradeep kaltari | last post by:
Hi All, I was just going through EMP table in oracle Scott schema. Say, I want to find out the first employee in alphabetical order who's name starts with 'A'. the following query gives me the...
3
by: bravo | last post by:
hi , i need to know whether there is any alternative method through which i can get the result of the querries like below in my sql... select * from table_name where rownum=1 and rowid not in...
0
by: Walt | last post by:
Darta wrote: I haven't read the question in detail, so this may be way off base, but have you considered using rowid instead of rownum? rownum is a psuedo-column that's a bit "slippery", i.e....
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.