Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

SQL select most recent records

Original Message
Name: Shmanda
Date: April 12, 2005 at 13:08:51 Pacific
Subject: SQL select most recent records
OS: WinXPPro
CPU/Ram: plenty
Comment:
Hi. I have a table we'll call PhoneTable:

ID Phone LastUpdated

999999 555-1234 1/1/01
999999 555-3456 4/21/02
999999 555-7890 9/18/04
999998 555-9998 4/20/05

I'd like the output to give me only the most recently updated records, so, the highest LastUpdated date for each ID:

999999 555-7890 9/18/04
999998 555-9998 4/20/05

Here's what I tried:
SELECT ID, Phone, MAX(LastUpdated)
FROM PhoneTable
GROUP BY ID, Phone, LastUpdated

But it's still giving me all of the rows. What am I missing?

Thanks!


Report Offensive Message For Removal


Response Number 1
Name: Michael J (by mjdamato)
Date: April 12, 2005 at 13:54:13 Pacific
Subject: SQL select most recent records
Reply: (edit)
Try using the DISTINCT clause instead with an ORDER BY on the date:

SELECT ID, Phone, DISTINCT LastUpdated
FROM PhoneTable
ORDER BY LastUpdated

Michael J


Report Offensive Follow Up For Removal

Response Number 2
Name: Shmanda
Date: April 12, 2005 at 14:23:33 Pacific
Subject: SQL select most recent records
Reply: (edit)
Thanks. I tried that but it still returns all of the rows, just in order of the most recent dates first. I still have the problem of dupes. If I select distinct on only the LastUpdated dates or only on IDs I get unique values but when I add LastUpdated to select it gives me duplicates. Any other ideas?

Report Offensive Follow Up For Removal

Response Number 3
Name: Michael J (by mjdamato)
Date: April 12, 2005 at 21:34:53 Pacific
Subject: SQL select most recent records
Reply: (edit)
If using distinct is giving you dupes, then add a group by. I'd try using distinct just on the LastUpdated and group by that as well.

Michael J


Report Offensive Follow Up For Removal

Response Number 4
Name: Euan
Date: May 26, 2005 at 06:31:17 Pacific
Subject: SQL select most recent records
Reply: (edit)
Try this

SELECT TOP 1 ID, Phone
FROM PhoneTable
ORDER BY LastUpdated DESC

This returns the records with the most recent first and the TOP function gives you only the first record. It works in SQL Server (and should do in Access although I havent tested it), if you are using MySQL or Oracle use the LIMIT function instead of TOP.


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: SQL select most recent records

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




DSHUB24 Connection Problems

need help with dsl and dial up

novel 3.12

help mandriva install last straw!

Icon Scaling in Explorer Bar


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC