473,378 Members | 1,397 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,378 software developers and data experts.

to fetch first record

hi,
can someone plz help me on this one
i need to fetch the first record from every group of records with the
same emp id.
i cannot use group by because i want to fetch all the fields
corresponding to a particular empid.
plz suggest a solution for this one

regards,
Sangram

Mar 23 '06 #1
14 16695
can you post some talbe definition and data?

Mar 23 '06 #2
try db2 olap function rank/row_number over(partition by ...)

Mar 23 '06 #3
as a example, first double or triple your staff table in sample
database.

then run:

with data as
(
select row_number() over (partition by id) as num, id, name from staff
)
select id, name from data where num = 1
;

is this way your want?

Mar 23 '06 #4
thx mate
i got it

Mar 23 '06 #5
>i cannot use group by because i want to fetch all the fields
corresponding to a particular empid


Yes you can. :)

SELECT * FROM table WHERE (empid, col2) IN
(SELECT empid, MIN(col2) FROM TABLE GROUP BY empid)
B.

Mar 23 '06 #6
Brian,

Depending on the data in the table, that is more akin to RANK than
ROW_NUMBER. That is, you might get multiple rows for each empid using
that method - if there are mutliple rows with the same MIN(col2) for a
given empid.

Of course, if this isn't the case, then it works just fine.

-Chris

Mar 23 '06 #7
I thought of the same thing. :)

However, he said he couldn't use GROUP BY because he wanted the rest of
the records too. From that i inferred that had he only wanted one
column or so, GROUP BY would work. Therefore, i provided my answer.

B.

Mar 23 '06 #8
Brian Tkatch wrote:
i cannot use group by because i want to fetch all the fields
corresponding to a particular empid


Yes you can. :)

SELECT * FROM table WHERE (empid, col2) IN
(SELECT empid, MIN(col2) FROM TABLE GROUP BY empid)


But you _can_ also do without the GROUP BY. For example like here:

Data:
-----
$ db2 "select * from t1"

A B
----------- -----------
1 2
1 3
1 4
1 1
2 1
2 2
2 8

This will now get the first two rows for each group:

SELECT o.*
FROM t1 AS o,
LATERAL ( SELECT *
FROM t1 AS i
WHERE o.a = i.a
ORDER BY i.b
FETCH FIRST 2 ROWS ONLY ) AS x
WHERE x.b = o.b

A B
----------- -----------
1 2
1 3
2 1
2 2

4 record(s) selected.

Granted, the GROUP BY is much, much nicer. But I just remembered that Serge
mentioned LATERAL once and I thought this should work as well. ;-)

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Mar 23 '06 #9
OK, another word i need to go look up. :)

Yeah, GROUP BY is much nicer. And, once someone actually understands
GROUP BY (easy conceot, but not taught well) it makes the reason the
statement does what it does very clear.

B.

Mar 23 '06 #10
Knut,

Trying this out myself (because I couldn't understand the results you
posted), I get a slightly different results:

A B
----------- -----------
1 2
1 1
2 1
2 2

Was this just a copying error, or is it really returning different
results for you?

-Chris

Mar 23 '06 #11
Chris wrote:
Knut,

Trying this out myself (because I couldn't understand the results you
posted), I get a slightly different results:

A B
----------- -----------
1 2
1 1
2 1
2 2

Was this just a copying error, or is it really returning different
results for you?


Your results are correct. When I first run the query (and copied the
results), I didn't have a (1, 1) row in there. This I added afterwards and
put it as sample data above the results. My fault.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Mar 24 '06 #12
Sangram,

would it be suitable for you?
(but perhaps too late ;)

SELECT DISTINCT B.*
FROM (SELECT 1, 2 UNION ALL
SELECT 1, 3 UNION ALL
SELECT 1, 4 UNION ALL
SELECT 1, 1 UNION ALL
SELECT 2, 1 UNION ALL
SELECT 2, 2 UNION ALL
SELECT 2, 8) AS B(c1, c2)
WHERE NOT EXISTS(SELECT *
FROM (SELECT 1, 2 UNION ALL
SELECT 1, 3 UNION ALL
SELECT 1, 4 UNION ALL
SELECT 1, 1 UNION ALL
SELECT 2, 1 UNION ALL
SELECT 2, 2 UNION ALL
SELECT 2, 8) AS P(c1, c2)
WHERE P.c1 = B.c1
AND P.c2 > B.c2)
---
Andrey Odegov
av******@yandex.ru
(remove GOV to respond)

Mar 30 '06 #13
Andrey Odegov wrote:
Sangram,

would it be suitable for you?
(but perhaps too late ;)

SELECT DISTINCT B.*
FROM (SELECT 1, 2 UNION ALL
SELECT 1, 3 UNION ALL
SELECT 1, 4 UNION ALL
SELECT 1, 1 UNION ALL
SELECT 2, 1 UNION ALL
SELECT 2, 2 UNION ALL
SELECT 2, 8) AS B(c1, c2)
WHERE NOT EXISTS(SELECT *
FROM (SELECT 1, 2 UNION ALL
SELECT 1, 3 UNION ALL
SELECT 1, 4 UNION ALL
SELECT 1, 1 UNION ALL
SELECT 2, 1 UNION ALL
SELECT 2, 2 UNION ALL
SELECT 2, 8) AS P(c1, c2)
WHERE P.c1 = B.c1
AND P.c2 > B.c2)

FY:
(SELECT 1, 2 UNION ALL
SELECT 1, 3 UNION ALL
SELECT 1, 4 UNION ALL
SELECT 1, 1 UNION ALL
SELECT 2, 1 UNION ALL
SELECT 2, 2 UNION ALL
SELECT 2, 8) AS B(c1, c2)
Is SQL server syntax. SQL Standard syntax is:
(VALUES (1, 2),
(1, 3),
(1, 1),
(2, 1),
(2, 2),
(2, 8)) AS B(c1, c2)
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Mar 30 '06 #14
Thanx Serge
i got it
i will improve in future :)
---
Andrey Odegov
av******@yandex.ru
(remove GOV to respond)

Mar 31 '06 #15

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

Similar topics

17
by: Rick | last post by:
Hi all, Is there a MySQL function to get the first record through a query? I would like to open a page of client records with the first one shown. The other records can be accessed from a...
8
by: Evan Smith | last post by:
During a routine performance check using an event monitor, I discovered a class of query whose performance has me baffled. The monitor captured: SELECT * FROM EWM_CASE fetch first 1 rows only...
2
by: deebeetwo | last post by:
I am trying to select only a certain number of records from a table. It is easy enough with "fetch first" syntax. What complicates this a bit is the fact that I need to retrieve the number of rows...
13
by: RR | last post by:
Hi, It appears that DB2 has an arbitrary restriction on the use of "fetch first N rows". I want to insert into a table from a large table query, but only insert the first N rows: insert...
9
by: Acupuncture | last post by:
Hi, I am developing a JDBC application and I encountered this problem (DB2 for ISeries). I want to do a select for update and also use the fetch first rows clause. This is my sql statement: ...
0
by: hagar | last post by:
Hi all, I have a problem which I can not understand why this is happening! Debugging this I actually see that it grabs first record then when stepping through code to the line rsImportTo.AddNew...
15
by: dataguy | last post by:
I am trying to insert into a temporary table but only the first n number of rows. I thought I could use the combination of insert into and fect first row command ,but it won't work. Does anyone...
2
by: Anees | last post by:
hi, am using mysql_fetch_array() method to fetch all the record and list it. after finishing all the records fetched i need to Index the record position into the beginning of the record set. ...
1
kaleeswaran
by: kaleeswaran | last post by:
hi! i am trying to get the very first record in cursor in mysql. i don't have knowledge in this.so any one give me some solution.... thank you, ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.