Ankit Agarwal

Ankit Agarwal

  • 1.6k
  • 307
  • 171.9k

Date sorting issue in sql server

Feb 15 2018 4:15 AM
Hello,
 
I want to display date, according to last updated but my date sorting is wrong.
 
I need to Result:-
 
Months Request
February 18 4
January 18 60
December 17 32
November 17 7
 
when i tried:-
 
select FORMAT(CreatedDateTime,'MMMM yy') as Months, count(*) as Request from LRequests
group by FORMAT(CreatedDateTime,'MMMM yy')
order by FORMAT(CreatedDateTime,'MMMM yy') DESC
 
so data displaying:
 
Months Request
November 17 7
January 18 60
February 18 4
December 17 32
 
then when i tried:-
 
select FORMAT(CreatedDateTime,'MMMM yy') as Months, count(*) as Request from LRequests
group by FORMAT(CreatedDateTime,'MMMM yy')
order by CreatedDateTime DESC
 
Getting error:-
 
Column "LRequests.CreatedDateTime" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
 
Please help me.
 
Thanks in Advance.
Ankit Agarwal
Software Engineer

Answers (2)