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

Polachan Paily 226 Reputation points
2021-04-30T14:35:14.057+00:00

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

Developer technologies | Transact-SQL
{count} votes

2 answers

Sort by: Most helpful
  1. Tom Cooper 8,481 Reputation points
    2021-04-30T14:39:11.803+00:00

    Just
    SELECT DISTINCT RegDate from Employees Order By RegDate

    Tom

    0 comments No comments

  2. EchoLiu-MSFT 14,621 Reputation points
    2021-05-03T01:43:23.397+00:00

    Hi @Polachan Paily ,

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.