Solved

SQL Query to display last row inserted

Posted on 2015-01-09
8
127 Views
Last Modified: 2015-01-09
I have a SQL 2005 Database. I'm looking for a relatively simple query to do the following. This is a very simplified example of the table. There are obviously more than two unique SystemNumbers.

TableName is Data

DataID         SystemNumber       TimeStamp
27765178    8001                          11:00 AM
27765179    8001                          11:02 AM
....
2777192      8555                          10:32 AM
2777193      8555                          10:37 AM

I need a query that can be run that will display the results like so:

DataID       SystemNumber       TimeStamp
27765178  8001                          11:02 AM
2777193    8555                          10:37 AM

So the most recent TImeStamp for each unique SystemNumber.

Any suggestions on how I can do this?

Eventually I will be using this on some SQL 2000 (I know) servers too, and putting into SQL Server Reporting
0
Comment
Question by:KThrace
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40540361
>So the most recent TImeStamp for each unique SystemNumber.
Give this a whirl.
Subquery gets the max time stamp for a given SystemNumber
Main query gets all rows in Data that match the above two values.
SELECT d.DataID, d.SystemNumber, dm.time_max as TimeStamp
FROM Data d
   JOIN (SELECT SystemNumber, Max(TimeStamp) as time_max 
         FROM Data
         GROUP BY SystemNumber) dm ON d.SystemNumber = dm.SystemNumber AND d.TimeStamp = dm.time_max
ORDER BY d.SystemNumber

Open in new window


For more examples see SQL Server GROUP BY Solutions,
0
 

Author Comment

by:KThrace
ID: 40540402
Beautiful! Works on 2005 AND 2000! I'll accept as a solution but as a follow up...

...for whatever reason in the table I have different DataID but same SystemNumber and EXACTLY same TimeStamp - so the result actually brings back two rows for each unique systemnumber. Is there a quick and easy way to add a statement such that:

If SystemNumber AND TimeStamp are the same return only one result
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40540410
How would you determine which 'one' to display?  Min(DataID), Max(DataID), I don't care just pick one, ...
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:KThrace
ID: 40540411
Max(DataID)
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40540436
Try this then.
SELECT Max(d.DataID), d.SystemNumber, dm.time_max as TimeStamp
FROM Data d
   JOIN (SELECT SystemNumber, Max(TimeStamp) as time_max 
         FROM Data
         GROUP BY SystemNumber) dm ON d.SystemNumber = dm.SystemNumber AND d.TimeStamp = dm.time_max
GROUP BY d.SystemNumber, dm.time_max
ORDER BY d.SystemNumber

Open in new window

0
 

Author Comment

by:KThrace
ID: 40540452
Jim, it's perfect!

Thank you so much!
0
 

Author Closing Comment

by:KThrace
ID: 40540455
Perfect and elegant!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40540461
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question