Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 16th, 2006, 06:14 AM
Registered User
 
Join Date: Mar 2006
Location: , , .
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!

Reply With Quote
  #2 (permalink)  
Old March 20th, 2006, 01:13 AM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
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
Reply With Quote
  #3 (permalink)  
Old December 13th, 2006, 01:59 PM
Registered User
 
Join Date: Dec 2006
Location: , , .
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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:10 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.