Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi Everyone,I need some help from a SQL guru! I have a table that consists of 10,000's of records. I need to return the most recent entry by time and code for that day. I know I can get what I want using MS SQL 2005 ROW_NUMBER OVER() but I have no idea how I can do this in MS SQL 2000 e.g.DATA:2008-02-01 10:00AM ASX12008-02-01 10:30AM ASX12008-02-02 10:00AM ASX12008-02-02 10:30AM ASX12008-02-03 10:00AM ASX12008-02-03 10:30AM ASX12008-02-04 10:00AM ASX22008-02-04 10:30AM ASX2EXPECTED RESULTS FOR ASX1:2008-02-03 10:30AM ASX12008-02-02 10:30AM ASX12008-02-01 10:30AM ASX1I would like to rank each result in a nested query and then only select the top n. Any help or a solution would be much appreciated!Thanks in advance!
jdaman
Constraint Violating Yak Guru
354 Posts
Posted - 2008-02-08 : 16:55:11
Have you tried using RANK() in place of ROW_NUMBER() with the same OVER() statement?
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-02-08 : 23:08:27
Try this:-
SELECT tmp.DateField,tmp.TextFieldFROM(SELECT DateField, TextField, (SELECT COUNT(*) + 1 FROM Table WHERE TextField=t.TextField AND DATEADD(d,DATEDIFF(d,0,DateField),0)= DATEADD(d,DATEDIFF(d,0,t.DateField),0) AND DateField>t.DateField) AS RowNoFROM Table t)tmpWHERE tmp.RowNo=1
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2008-02-09 : 01:51:03
orselect max(DateField),TextField from Table group by dateadd(day,datediff(day,0,DateField),0),TextFieldMadhivananFailing to plan is Planning to fail