SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Help with SQL Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dimepop
Starting Member

33 Posts

Posted - 07/11/2013 :  10:08:17  Show Profile  Reply with Quote
Hi i have a table with columns
[name] ,[type],[ttl],[ip],[Timestamp] ,[office],[source],[outside],[ping],[status],[milliseconds]
There are 10 distinct names
I want to get a query that will display some columns of each name which have the latest timestamp (smalldatetime)
So my query will display the unique names with the latest timestamp and whatever is in the other columns.

I tried with this query but it shows jultiple names for each ....

SELECT name, ping, office, ip, status, milliseconds, max(Timestamp) as time
FROM table


group by name, ping, office, ip,status,milliseconds
order by name

What would be the best query?
Thanks


James K
Flowing Fount of Yak Knowledge

3557 Posts

Posted - 07/11/2013 :  10:20:58  Show Profile  Reply with Quote
SELECT name, ping, office, ip, status, milliseconds,TIME
FROM
(
	SELECT name, ping, office, ip, status, milliseconds,Timestamp as TIME,
		ROW_NUMBER() OVER (PARTITION BY name ORDER BY TIMESTAMP DESC) AS RN
	FROM TABLE
) s WHERE RN = 1

Edited by - James K on 07/11/2013 10:21:09
Go to Top of Page

dimepop
Starting Member

33 Posts

Posted - 07/11/2013 :  11:04:44  Show Profile  Reply with Quote
YES James K, you got it.
Thanks
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000