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 March 16th, 2006, 06:14 AM
Registered User
 
Join Date: Mar 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL query retrieving last record and group by

Hi,

Can anyone help me on this?
I have a table with the following fields: ID (unique), bID, type, value and createddate

the data type are uniqueidentifier, uniqueidentifier, varchar, ntext and datetime respectively.

sample data:
ID bID type value createddate
ui1 ui0 field1 value1 2006-02-13 16:39:21.100
ui2 ui0 field1 value2 2006-02-20 18:00:00.100
ui3 ui0 field2 value3 2006-02-13 16:39:21.200
ui4 ui0 field2 value4 2006-02-20 18:00:00.200

I'll need to retrieve the latest value record for each type.

Hence the expected result should be like this:
ID bID type value createddate
ui2 ui0 field1 value2 2006-02-20 18:00:00.100
ui4 ui0 field2 value4 2006-02-20 18:00:00.200

I am aware that since the datatype value is ntext, i cannot do comparison of that field together with the max() function.

I hope to carry out the query in a single query instead of separate 2 queries that I thought of.

Pls advise. thxs in advance!

 
Old March 20th, 2006, 01:13 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Try:

select a.ID, a.bID, a.type, a.value, a.createddate
from aaaa A
inner join
( select type, max(createddate) as maxdate
    from aaaa
    group by type
) B on A.type=B.type and A.createddate=B.maxDate

Jim
 
Old December 13th, 2006, 01:59 PM
Registered User
 
Join Date: Dec 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The routine as listed works for a similar problem i have, except I need to add a limiting date parameter. ex: exclude records with a date > date_parameter and then take the last record.

Is this possible?

Thanks,
Dan






Similar Threads
Thread Thread Starter Forum Replies Last Post
One Record From Each Group - Query rstelma SQL Server 2000 7 January 3rd, 2008 12:08 AM
SQL query using "Group By" - please help BananaJim SQL Language 2 February 26th, 2007 10:23 AM
Not reading last record group. geordaa XSLT 2 July 13th, 2006 02:35 AM
Selecting top record of a group by clause lic023 Access 7 June 7th, 2006 11:25 AM
SQL Query - picking latest record and group by markw SQL Language 2 April 6th, 2005 03:54 AM





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