Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help in SELECT Query

Author  Topic 

mahdi87_gh
Yak Posting Veteran

72 Posts

Posted - 2011-03-31 : 02:55:20
Hi
I have a Table like below:
RegisterChanges (regID,computerID,computerName,ip,date,time)

i want to select the last record for each computerID. consider i don't want to have two records with the same computerID.
for example if we have three records like below in the table:
1,001,pc1,2010/05/20
2,002,pc2,2010/06/01
3,001,pc1,2011/01/01


in the results we should have only rows number 2 and 3


thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-03-31 : 03:22:59
select * from
(select row_number() over (partition by computerID order by date desc,time desc) as rownum,* from RegisterChanges) as dt
where rownum=1


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

mahdi87_gh
Yak Posting Veteran

72 Posts

Posted - 2011-03-31 : 03:44:50
Thanks webfred
in the mean time, i write this query by myself. how can i find-out which query have a better performance? does sql have a tool for this?
my query is :

SELECT t.regID,t.computerID,t.computerName,t.ip,t.date,t.time FROM RegisterChanges t
INNER JOIN (
SELECT r.computerID,MAX(r.date) as date
FROM RegisterChanges r
GROUP BY r.computerID
) s
ON t.computerID=s.computerID AND t.date=s.date
ORDER BY t.computerID

Go to Top of Page

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-03-31 : 08:14:36
quote:
how can i find-out which query have a better performance?



If you are using SQLServer Management Studio
select Both Queries( which you want to compare) and Press Ctrl+L (in a menu bar Query-->Display Estimated Execution PLan)
it will show Query cast in batch wise
Go to Top of Page
   

- Advertisement -