Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 19th, 2005, 07:15 AM
Registered User
 
Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select From Group?

Hi Guys, can anyone help me with this please?

I have some data in the form:

Code:
ID      Sequence   Field1   Field2
1234       1         x         y
1234       2         x         z
1234       3         a         q
5432       1         q         w
5432       2         a         w
5432       3         e         e

How can I group the above records by ID and select the records with the largest sequence number in each group to give:

Code:
ID      Sequence  Field1   Field2
1234       3       a         q
5432       3       e         e
I can't use the GROUP BY or DISTINCT clauses as this will aggregate the records in each group, which is not an option.

Any ideas?

Thanks in advance,


Chris

 
Old September 19th, 2005, 12:28 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can do this using a (correlated) subquery. It's best to work these from the "inside out". First create a query which returns the Sequence number you want (the maximum) for any given ID:
Code:
    SELECT MAX(Sequence) FROM YourTable WHERE ID = <a given ID>
Then, you want to select the row, in each subset of rows for a given ID, that has this Sequence (in pseudocode):
Code:
    SELECT ID, Sequence, Field1, Field2
    FROM YourTable
    WHERE ID = <the ID I'm currently on>
        AND <the Sequence is a maximum>
Combine the two and correlate the two queries on the ID:
Code:
    SELECT ID, Sequence, Field1, Field2
    FROM YourTable T1
    WHERE Sequence = SELECT MAX(Sequence) FROM YourTable T2 WHERE T1.ID = T2.ID
...should do the trick.



Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old September 21st, 2005, 06:33 AM
Registered User
 
Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's great.

Thank you Jeff.


Cheers,


Chris

 
Old October 12th, 2005, 12:05 AM
Registered User
 
Join Date: Aug 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

select ID,max(sequence),field1,field2 from table
group by id
order by id;

 
Old October 12th, 2005, 05:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by iverson_wuwei
 select ID,max(sequence),field1,field2 from table
group by id
order by id;
And if you try that, you'll get an error, since that query is not valid SQL.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Group Within another Group, xslt1.0 jhansib4u BOOK: XSLT Programmer's Reference, 2nd Edition 4 November 22nd, 2007 01:24 AM
Restart new group number in Group Footer sukarso Crystal Reports 2 October 13th, 2006 12:11 PM
Select min(price) Group By sku = duplicates Stuart Stalker SQL Server DTS 2 March 6th, 2006 01:36 AM
Fill select box and select recordset value markd Classic ASP Databases 1 February 20th, 2006 06:41 PM
Group by , Sub Group by and Sum mateenmohd SQL Server 2000 1 March 29th, 2005 09:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.