I have table:
CREATE TABLE [dbo].[test] (
[name] nvarchar(max) NULL,
[date] datetime NULL
)
And records on it:
a 2010-09-02 12:00:00
a 2010-09-02 11:00:00
b 2010-09-02 12:00:00
b 2010-09-02 11:00:00
And i want to get all name with the newest date:
I may do:
select t.[name] from test t
group by t.[name]
having max(date) = (select MAX(DATE) from test where [name] = t.[name])
which have one problem – i can’t get a date
I may do:
select t.*
from test t
where t.[date] = (select MAX(DATE) from test where [name] = t.[name])
which hasn’t any problem
My question is:
May i do this better?? I will fetch around 10,000 records from table incremental table(every day 10,000 more results).
Regards
What version of SQL Server?
SQL 2005 and up:
SQL 2000:
If you can have duplicate dates then another step is needed for the sql 2000 version to get it down to one row.
@Oded pointed out that you can simply get the max date. If all you need are the name and date then his query is best. But if my suspicion is correct that you need more items from the same row, then you’ll need queries like these.
Here’s another SQL 2005 version:
This query will have problems with duplicate max dates for the same name
Update
Now that I know it’s SQL 2008:
The row_number() solution is simplest and easiest. I’d start with that. If performance isn’t enough, and the table is a child of a parent table that has each [name] only once, try the CROSS APPLY solution with the outer table (test T) as the parent:
If there is no parent table, you can try the above queries or use
SELECT DISTINCT [name] FROM testbut I’m not convinced that will be a performance improvement: