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 | 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 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
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 2nd, 2008, 03:13 AM
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting distinct and latest record

Hi,

Can anyone please help me extract distinct and latest records from a table?

To illustruate, let's say I have the table below:

    ref_no name
      1 santos
      2 garcia
      3 santos
      4 david
      5 bea
      6 garcia
      7 santos

From this table, I would need to have the results below:

   ref_no name
      4 david
      5 bea
      6 garcia
      7 santos

The results must display distinct and lates records based on ref_no.

Thank you so much in advance!


Reply With Quote
  #2 (permalink)  
Old May 2nd, 2008, 06:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

The way approach things like this is to break it down into steps, usually from the "inside out". First, you want to find the set of ref_no where it is a maximum for each name:

SELECT MAX(ref_no) FROM yourtable GROUP BY name;

Them you want the rows in your table where the ref_no is in this set:

SELECT ref_no, name FROM yourtable
WHERE ref_no IN (SELECT MAX(ref_no) FROM yourtable GROUP BY name);

This resultset will be in an arbitrary order, so you may want to order it:

SELECT ref_no, name FROM yourtable
WHERE ref_no IN (SELECT MAX(ref_no) FROM yourtable GROUP BY name)
ORDER BY ref_no;





Jeff Mason
je.mason@comcast.net
Reply With Quote
  #3 (permalink)  
Old May 2nd, 2008, 08:51 AM
Friend of Wrox
 
Join Date: Oct 2004
Location: delhi, delhi, India.
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hi ElyGp!!

Another way to achieve it..
SELECT MAX(ref_no), MAX(name)
FROM dbo.tempTbl
GROUP BY name
ORDER BY MAX(ref_no)

Note-I do recommend you not to use In clause in query for performance reason.
Please let me know if you have any other alternatives
Hope this will help you


Cheers :)

vinod
Reply With Quote
  #4 (permalink)  
Old May 2nd, 2008, 09:45 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by vinod_yadav1919

Another way to achieve it..
SELECT MAX(ref_no), MAX(name)
FROM dbo.tempTbl
GROUP BY name
ORDER BY MAX(ref_no)
You are right; the grouping you show above is more efficient than using the IN clause for the specific two column result the OP requested. But, if there were other columns in the table that needed to be retrieved along with the ref_no and name columns, the grouping operation starts to break down.

For example, if the table contained, say, a character string comment column in addition to the two already defined, and the query was to return the comment associated with the max ref_no for each name, the grouping wouldn't work...


Jeff Mason
je.mason@comcast.net
Reply With Quote
  #5 (permalink)  
Old May 4th, 2008, 10:49 PM
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks to both of you guys!! I really appreciate your suggestions! :)


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
Distinct SELECT DISTINCT question... EndEffect Classic ASP Databases 4 August 18th, 2005 08:53 AM
get latest record frm transaction tbl jyopallavi SQL Server 2000 1 June 9th, 2005 08:11 AM
Retreive latest and greatest record alexvgs Oracle 3 April 25th, 2005 09:57 PM
SQL Query - picking latest record and group by markw SQL Language 2 April 6th, 2005 03:54 AM
What's the latest C# edition? Daemon BOOK: ASP.NET Website Programming Problem-Design-Solution 1 January 18th, 2004 05:53 PM



All times are GMT -4. The time now is 05:45 PM.


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