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 3 4604
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: - Select
-
ROW_NUMBER() OVER(order by some_field asc) as ROWNO,
-
some,other,fields
-
From
-
employee_table
-
WHERE
-
ROWNO>30
-
ANND
-
ROWNO<=50
-
;
Please let us know if DB2 v5 is able to work like this.
Regards,
Bernd
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: - Select
-
ROW_NUMBER() OVER(order by some_field asc) as ROWNO,
-
some,other,fields
-
From
-
employee_table
-
WHERE
-
ROWNO>30
-
ANND
-
ROWNO<=50
-
;
Please let us know if DB2 v5 is able to work like this.
Regards,
Bernd
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: - select
-
*
-
from
-
employee
-
where
-
-- e_id>30
-
e_id>(
-
select
-
max(tmptbl1.e_id)
-
from
-
(
-
select
-
e_id
-
from
-
employee
-
order by
-
e_id asc
-
fetch first 30 rows only
-
) tmptbl1
-
)
-
-
and
-
-- e_id<=50
-
e_id<=(
-
select
-
max(tmptbl2.e_id)
-
from
-
(
-
select
-
e_id
-
from
-
employee
-
order by
-
e_id asc
-
fetch first 50 rows only
-
) tmptbl2
-
)
-
-
order by e_id asc
-
;
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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)
|
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,...
|
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...
|
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???
...
|
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...
|
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?
|
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...
|
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...
|
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....
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |