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)
 select problem

Author  Topic 

louise.hansare.gmail.com
Starting Member

1 Post

Posted - 2010-06-09 : 06:18:43
Hi,
I am really desperate now. I have one table with an auditlog that has a dataid and a date. I have another table with dataid and name. From the second table I can fetch the names I need with a select-statement as there is a where-statement needed.

I need the top five distinct names ordered by how they show in the audit-table.

Name - Date desc
Name2 - closest date to now
Name5 -
Name3 -

LSwe

Sachin.Nand

2937 Posts

Posted - 2010-06-09 : 06:23:49
Post the structure of your tables,some sample data and the desired output.

Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-09 : 06:24:20
select top 5
nt.name,
dt.date
from (select dataid,max(date) as date from auditlog group by dataid)dt
join nametable nt on nt.dataid=dt.dataid
order by dt.date desc


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

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-09 : 06:25:40
There is also a possible solution using row_number() but I see no reason to do that.


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

- Advertisement -