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.
Author |
Topic |
mahdi87_gh
Yak Posting Veteran
72 Posts |
Posted - 2011-03-31 : 02:55:20
|
HiI 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/202,002,pc2,2010/06/013,001,pc1,2011/01/01 in the results we should have only rows number 2 and 3thanks |
|
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 dtwhere rownum=1 No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
mahdi87_gh
Yak Posting Veteran
72 Posts |
Posted - 2011-03-31 : 03:44:50
|
Thanks webfredin 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 tINNER JOIN (SELECT r.computerID,MAX(r.date) as dateFROM RegisterChanges rGROUP BY r.computerID) s ON t.computerID=s.computerID AND t.date=s.dateORDER BY t.computerID |
 |
|
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 |
 |
|
|
|
|
|
|