MojoDK
MojoDK

Reputation: 4528

TSQL: Returning customers by date descending?

Each time my user looks up a customer, I store the customer ID, Name and timestamp (timestamp = when the user performed the look up).

Kinda like:

ID  Name      Timestamp
1   CompanyA  2012-10-01 10:00
2   ComapnyB  2012-10-01 10:11
3   CompanyA  2012-10-01 10:22
4   CompanyA  2012-10-01 10:25
4   CompanyC  2012-10-01 10:32

My question is ...

I want to return TOP 30 distinct customers sorted by date descending - how do I do that?

I want to return this:

CompanyC
CompanyA
CompanyB

... only a single instance sorted by the date descending.

Upvotes: 3

Views: 94

Answers (1)

Michael Fredrickson
Michael Fredrickson

Reputation: 37398

SELECT TOP 30 Name
FROM Customer
GROUP BY Name
ORDER BY MAX(Timestamp) DESC

Upvotes: 5

Related Questions