0

My sql query is:

SELECT DISTINCT
   SUBSTRING(DATENAME(MONTH, PostDate), 1, 3) + '-' + CAST(YEAR(PostDate) AS VARCHAR(4)) AS PostArchive,
   Posts = COUNT(*) 
FROM    
   Post  WHERE Verified=1 
GROUP BY
   SUBSTRING(DATENAME(MONTH, PostDate), 1, 3) + '-' + CAST(YEAR(PostDate) AS VARCHAR(4)),
   YEAR(PostDate), MONTH(PostDate)

 ORDER BY PostArchive

Its gives a result like this:

PostArchive    Posts
------------------------
Mar-2009    1
Mar-2010    1
May-2005    1
May-2011    1
May-2012    1
May-2013    1

But I want a result order by date(year) like this.

PostArchive    Posts
------------------------
May-2005    1
Mar-2009    1
Mar-2010    1
May-2011    1
May-2012    1
May-2013    1

I search and found this link but unable to solve my problem.

I try :

ORDER BY CONVERT(DateTime, PostArchive,101)  DESC

But it gives me a error:

Invalid column name 'PostArchive'.

Is there any way to do this or I am in wrong way.Thanks.

4

5 回答 5

2

The reason for the error is that PostArchive is the name you've given to the column on the SELECT line, which is effectively the output of the query. The ORDER BY clause does not look at that, it looks at its input to the query, which in this case is PostDate

于 2013-05-05T12:00:00.767 回答
1
  1. I assume that you didn't really mean that you want to order it by year, but instead by year/month. The ordering issue that you have is because you are ordering it as a character and not as a date.
  2. You don't need DISTINCT, since you already GROUP BY.
  3. Main problem is that you already converted to VARCHAR. Hence, months are unsortable.

ssss

-- Create a CTE (inline view)

WITH T AS (
SELECT YEAR(PostDate) PostYear
     , MONTH(PostDate) PostMM
     , SUBSTRING(DATENAME(MONTH, PostDate),1,3) PostMonth
     , COUNT(*) Posts
  FROM Post
 WHERE Verified = 1
 GROUP BY YEAR(PostDate)
     , MONTH(PostDate)
     , DATENAME(MONTH, PostDate)
)
 -- Build you date string
SELECT PostMonth + '-' + CAST(PostYear AS VARCHAR(4)) AS PostArchive
     , Posts
  FROM T
   -- Sort it by the components separately
 ORDER BY PostYear
  -- Don't use the character, otherwise, Aug will come before Mar
     , PostMM
于 2013-05-05T15:51:52.553 回答
0

I used CTE to get the result try this

with tempTable (PostArchiveMonth , PostArchiveYear , PostArchiveMonthName ,  Posts )
(
    select month(PostDate) , YEAR(PostDate) , SUBSTRING(DATENAME(MONTH, PostDate), 1, 3) 
           COUNT(*) 
    FROM Post  
    WHERE Verified=1 
    group by MONTH(PostDate) ,YEAR( PostDate)
    ,SUBSTRING(DATENAME(MONTH, PostDate), 1, 3) 
)

select PostArchiveMonthName +'-' + PostArchiveYear as PostArchive , Posts
from  tempTable 
order by PostArchiveYear  , PostArchiveMonth 
于 2013-05-05T12:31:11.203 回答
0

Try

SELECT DISTINCT
   SUBSTRING(DATENAME(MONTH, PostDate), 1, 3) + '-' + CAST(YEAR(PostDate) AS VARCHAR(4)) AS PostArchive,
   Posts = COUNT(*) 
FROM    
   Post  WHERE Verified=1 
GROUP BY
   SUBSTRING(DATENAME(MONTH, PostDate), 1, 3) + '-' + CAST(YEAR(PostDate) AS VARCHAR(4)),
   YEAR(PostDate), MONTH(PostDate)
Order by Month(PostDate), Year(PostDate)
于 2013-05-05T12:35:35.773 回答
0

Try to change this:

ORDER BY PostArchive

...to this...

ORDER BY YEAR(PostDate)

于 2013-05-05T13:20:28.203 回答