Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / March 2006

Tip: Looking for answers? Try searching our database.

to fetch first record

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sangram.0149@gmail.com - 23 Mar 2006 07:06 GMT
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
Hardy - 23 Mar 2006 11:41 GMT
can you post some talbe definition and data?
Hardy - 23 Mar 2006 11:43 GMT
try db2 olap function rank/row_number over(partition by ...)
Hardy - 23 Mar 2006 11:51 GMT
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?
sangram.0149@gmail.com - 23 Mar 2006 14:06 GMT
thx mate
i got it
Brian Tkatch - 23 Mar 2006 15:52 GMT
>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.
Chris - 23 Mar 2006 17:13 GMT
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
Brian Tkatch - 23 Mar 2006 18:35 GMT
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.
Knut Stolze - 23 Mar 2006 21:47 GMT
>>i cannot use group by because i want to fetch all the fields
>>corresponding to a particular empid
[quoted text clipped - 3 lines]
> 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. ;-)

Signature

Knut Stolze
DB2 Information Integration Development
IBM Germany

Brian Tkatch - 23 Mar 2006 22:55 GMT
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.
Chris - 24 Mar 2006 00:17 GMT
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
Knut Stolze - 24 Mar 2006 11:10 GMT
> Knut,
>
[quoted text clipped - 10 lines]
> 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.

Signature

Knut Stolze
DB2 Information Integration Development
IBM Germany

Andrey Odegov - 30 Mar 2006 06:37 GMT
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
avodeGOV@yandex.ru
(remove GOV to respond)
Serge Rielau - 30 Mar 2006 12:20 GMT
> Sangram,
>
[quoted text clipped - 19 lines]
>                    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)
Signature

Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

Andrey Odegov - 31 Mar 2006 05:49 GMT
Thanx Serge
i got it
i will improve in future :)
---
Andrey Odegov
avodeGOV@yandex.ru
(remove GOV to respond)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.