Solved

SQL Query to display last row inserted

Posted on 2015-01-09
8
116 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
  • 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
 

Author Comment

by:KThrace
ID: 40540411
Max(DataID)
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now