WiXXeY
WiXXeY

Reputation: 991

Order By and Distinct in same SQL query

I am using sql server 2008, i have the following data

sNames             sDate 
(varchar(MAX))    (date)
==========     =============
 ALS           10/02/2012
 SSP           11/03/2012
 MRP           11/05/2012
 ALS           14/06/2012
 ALS           04/10/2012
 ALS           03/11/2012
 MRP           05/09/2012
 PPL           18/08/2012

I want to order the list by sDate in desc but must show distinct sNames. kindly guide me

Upvotes: 0

Views: 93

Answers (6)

zildjiean
zildjiean

Reputation: 1

select sName,sDate From [your_table] order by sDate Desc 

Upvotes: 0

Novice
Novice

Reputation: 558

SELECT DISTINCT sNames, sDate  
FROM <tableName>  
ORDER BY sDate DESC

Upvotes: 0

Sobhan
Sobhan

Reputation: 816

SELECT DISTINCT sNames FROM <YOUR TABLE NAME> ORDER BY sDate DESC

Upvotes: 0

Pranav
Pranav

Reputation: 8871

select max(sDate),sname from yourTable 
group by sname 
order by max(sDate) desc

Upvotes: 0

Nilesh Thakkar
Nilesh Thakkar

Reputation: 2895

Try below:

Select distinct snames 
from yourtable 
order by sdate desc

Upvotes: 0

juergen d
juergen d

Reputation: 204746

Using the latest dates for duplicate sNames you can do

select sNames, max(sDate)
from your_table
group by sNames
order by max(sDate) desc

Upvotes: 5

Related Questions