473,473 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

DB2 Query problem

Hi,

I have the following data in my DB2 table ("Employee") :

EMPID MONTH SALARY
E340 JAN 3000
E340 FEB 2000
E340 MAR 3000
E340 APR 4000
E341 JAN 3000
E341 FEB 2000
E341 MAR 3000
E341 APR 4000

In the above data, i have four records each for two employees with
employee-id E340 and E341.
My requirement is that i want the data for the third row of each
employee using only the Employee-id . I do not want to use a WHERE
clause for month in my select query.

The following SELECT query will give me all the four records for a
particular employee-id:
SELECT * FROM EMPLOYEE where EMPID='E340'.

I do not want all the four records but only the 3rd record for each
employee. Is there a way in which the above query can be modified to
provide only the third row data ?

Thanks for your time,
Sachin

Mar 24 '06 #1
7 1477
sunny wrote:
Hi,

I have the following data in my DB2 table ("Employee") :

EMPID MONTH SALARY
E340 JAN 3000
E340 FEB 2000
E340 MAR 3000
E340 APR 4000
E341 JAN 3000
E341 FEB 2000
E341 MAR 3000
E341 APR 4000

In the above data, i have four records each for two employees with
employee-id E340 and E341.
My requirement is that i want the data for the third row of each
employee using only the Employee-id . I do not want to use a WHERE
clause for month in my select query.

The following SELECT query will give me all the four records for a
particular employee-id:
SELECT * FROM EMPLOYEE where EMPID='E340'.

I do not want all the four records but only the 3rd record for each
employee. Is there a way in which the above query can be modified to
provide only the third row data ?

Thanks for your time,
Sachin

Third in what sense? Order is not defined for a table (including a
result table), until you specify an ORDER BY clause. Third month
ascending? Third highest/lowest salary? You could look at the RANK or
ROWNUMBER...OVER in the db2 docs at:
http://publib.boulder.ibm.com/infoce.../v8//index.jsp
Mar 24 '06 #2
>> My requirement is that i want the data for the third row of each employee using only the Employee-id .<<
Q1) Each employee-ids are same. So, how to distinguish each row.
Q2) What order of third row? If you don't care ordered by which column,
are there any meaning in the "third". third row ordered by MONTH and
third row ordered by SALARY may different.

Mar 24 '06 #3
Hi,

Let's assume that the data is ordered by month and the records for an
employee ar distinguished by month. I hope this will make it more clear
and undestandable,

Thanks,
Sachin

Mar 24 '06 #4
"sunny" <sa*************@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Hi,

Let's assume that the data is ordered by month and the records for an
employee ar distinguished by month. I hope this will make it more clear
and undestandable,

Thanks,
Sachin


In a relational database, there is no guarantee that the data will be
returned to the program in any particular order, or even returned in the
same order that the data is stored in, unless you specify "ORDER BY" (or
other equivalent statement).

There are no exceptions, decision of the judges is final, no refunds or
exchanges will be given.
Mar 24 '06 #5
Sachin,

Query :
select * from (select rownumber() over (order by empid) as row_number,
empid,salary from siebel.employee where empid='E340') as t where
row_number=3
And there is always more than one way to get work done!!!

Hi,

I have the following data in my DB2 table ("Employee") :

EMPID MONTH SALARY
E340 JAN 3000
E340 FEB 2000
E340 MAR 3000
E340 APR 4000
E341 JAN 3000
E341 FEB 2000
E341 MAR 3000
E341 APR 4000

In the above data, i have four records each for two employees with
employee-id E340 and E341.
My requirement is that i want the data for the third row of each
employee using only the Employee-id . I do not want to use a WHERE
clause for month in my select query.

The following SELECT query will give me all the four records for a
particular employee-id:
SELECT * FROM EMPLOYEE where EMPID='E340'.

I do not want all the four records but only the 3rd record for each
employee. Is there a way in which the above query can be modified to
provide only the third row data ?

Thanks for your time,
Sachin


Mar 24 '06 #6
Thanks a lot guys.

I tried using the resolution with ROW_NUMBER() but it turns out that
ROW_NUMBER() is not supported in OS/390 DB2 which i am using.
Is there a way of doing this without using the ROW_NUMBER() function.

Thanks,
Sachin

Mar 24 '06 #7
sunny wrote:
Thanks a lot guys.

I tried using the resolution with ROW_NUMBER() but it turns out that
ROW_NUMBER() is not supported in OS/390 DB2 which i am using.
Is there a way of doing this without using the ROW_NUMBER() function.

Thanks,
Sachin

Try this (untested)
SELECT EMPID, MONTH, SALARY
FROM EMPLOYEE E
WHERE EXISTS(SELECT COUNT(*)
FROM EMPLOYEE
WHERE E.EMPID = EMPID AND E.MONTH >= MONTH
GROUP BY EMPID, MONTH
HAVING COUNT(*) = 3)

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Mar 24 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP (running off Apache 1.3.31 w/ PHP 5.0.1) to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using...
3
by: Brian Oster | last post by:
After applying security patch MS03-031 (Sql server ver 8.00.818) a query that used to execute in under 2 seconds, now takes over 8 Minutes to complete. Any ideas on what the heck might be going...
1
by: Jeff Blee | last post by:
I hope someone can help me get this graph outputing in proper order. After help from Tom, I got a graph to display output from the previous 12 months and include the average of that output all in...
8
by: Adam Louis | last post by:
I would like help resolving this problem. I'm a novice who's been hired to query a hospital database and extract useful information, available to me only in a dynamically generated, downloadable...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
4
by: Apple | last post by:
1. I want to create an autonumber, my requirement is : 2005/0001 (Year/autonumber), which year & autonumber no. both can auto run. 2. I had create a query by making relation to a table & query,...
11
by: Andy_Khosravi | last post by:
My problem: I'm having trouble with a query taking much too long to run; a query without any criteria evaluating only 650 records takes over 300 seconds to run (over the network. On local drive...
4
by: Konrad Hammerer | last post by:
Hi! I have the following problem: I have a query (a) using another query (b) to get the amount of records of this other query (b), means: select count(MNR) as Number from...
4
by: Stan | last post by:
I am using MS Office Access 2003 (11.5614). My basic question is can I run a query of a query datasheet. I want to use more that one criteria and can not get that query to work. I thought I...
2
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes...
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...
1
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.