question

PolachanPaily-2805 avatar image
0 Votes"
PolachanPaily-2805 asked EchoLiu-msft commented

How can use Order by clause with Distinct in select of single column

I have the following sql

SELECT DISTINCT RegDate from Employees

In the above sql, how can I ORDER BY RegDate. If not possible , how can get distinct dates with order by

Thanks
Pol

sql-server-transact-sql
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Could you have any updates?

Echo

0 Votes 0 ·
TomCooper-6989 avatar image
0 Votes"
TomCooper-6989 answered

Just
SELECT DISTINCT RegDate from Employees Order By RegDate

Tom

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

EchoLiu-msft avatar image
0 Votes"
EchoLiu-msft answered EchoLiu-msft edited

Hi @PolachanPaily-2805,

According to the positive order of RegDate to get different dates:

     SELECT DISTINCT RegDate from Employees Order By RegDate ASC

Sort in reverse order according to RegDate:

     SELECT DISTINCT RegDate from Employees Order By RegDate DESC

The default sorting method of SQL Server is in accordance with the positive sequence of the fields. When ASC and DESC are omitted, they will be sorted in the positive order of the fields.

In other words, the following two pieces of code are equivalent:

     SELECT DISTINCT RegDate from Employees Order By RegDate
     SELECT DISTINCT RegDate from Employees Order By RegDate ASC

For more details, please refer to:
SELECT - ORDER BY Clause (Transact-SQL)


If you have any question, please feel free to let me know.


Regards
Echo


If the answer is helpful, please click "Accept Answer" and upvote it.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.