按升序排序月份

时间:2015-09-24 13:22:20

标签: sql sql-server

我查看了堆栈上的相关问题。 我试图将我的查询排序为按升序显示月份。 Stack上的一篇类似帖子说我必须指出" ASC",但这对我不起作用。我写了一个Order By。我无法弄清楚它为什么不起作用。

SELECT  DATENAME( MONTH,(Submission.SubmissionDate)) AS [Date]        
FROM Submission 
GROUP BY DATENAME( MONTH,(Submission.SubmissionDate)) ORDER BY [Date] ASC;

这是输出:

  Month
  August
 February
 September

注意:这只是我查询的一部分,我不认为有必要显示其余内容,这与我的表中的其他属性相关(不是月份相关)

编辑:这是我的整个查询:(初始一个)

SELECT  DATENAME( MONTH,(Submission.SubmissionDate)) AS [Date], 
SUM( CASE WHEN Submission.Status='Under review' THEN 1 ELSE 0 END) [Under_Review], 
SUM( CASE WHEN Submission.Status='Accepted' THEN 1 ELSE 0 END) [Accepted], 
SUM( CASE WHEN Submission.Status='Rejected' THEN 1 ELSE 0 END) [Rejected], 
SUM( CASE WHEN Submission.Status='In print' THEN 1 ELSE 0 END) [In_print], 
SUM( CASE WHEN Submission.Status='Published' THEN 1 ELSE 0 END) [Published] 
FROM Submission INNER JOIN ((Faculty INNER JOIN School ON Faculty.FacultyID = School.[FacultyID]) INNER JOIN (Researcher INNER JOIN ResearcherSubmission ON Researcher.ResearcherID = ResearcherSubmission.ResearcherID) ON School.SchoolID = Researcher.SchoolID) ON Submission.SubmissionID = ResearcherSubmission.SubmissionID 
GROUP BY DATENAME( MONTH,(Submission.SubmissionDate)) 
ORDER BY DATENAME( MONTH,(Submission.SubmissionDate));

3 个答案:

答案 0 :(得分:3)

SELECT  DATENAME( MONTH, Submission.SubmissionDate) AS [Date]        
FROM Submission 
ORDER BY datepart(mm,Submission.SubmissionDate)

您不需要group by(对于显示的查询)。此外,当您order by month name时,它会按字母顺序month name返回结果。您不应在aliaswhere order byhaving条款中使用先前定义的group by es。

编辑:问题在于join条件。你应该按照行中的评论来纠正它们。

SELECT DATENAME( MONTH,(Submission.SubmissionDate)) AS [Date], 
SUM( CASE WHEN Submission.Status='Under review' THEN 1 ELSE 0 END) [Under_Review], 
SUM( CASE WHEN Submission.Status='Accepted' THEN 1 ELSE 0 END) [Accepted], 
SUM( CASE WHEN Submission.Status='Rejected' THEN 1 ELSE 0 END) [Rejected], 
SUM( CASE WHEN Submission.Status='In print' THEN 1 ELSE 0 END) [In_print], 
SUM( CASE WHEN Submission.Status='Published' THEN 1 ELSE 0 END) [Published] 
FROM Faculty 
INNER JOIN School ON Faculty.FacultyID = School.[FacultyID]
INNER JOIN Researcher ON School.SchoolID = Researcher.SchoolID
INNER JOIN ResearcherSubmission ON Researcher.ResearcherID = ResearcherSubmission.ResearcherID 
INNER JOIN SUBMISSION ON Submission.SubmissionID = ResearcherSubmission.SubmissionID 
GROUP BY DATENAME( MONTH,(Submission.SubmissionDate)) 
ORDER BY DATEPART( MONTH,(Submission.SubmissionDate))

答案 1 :(得分:1)

一个简单的选择是将MONTH(Submission.SubmissionDate)添加到您的group by子句中,并将order by添加到其中:

SELECT  DATENAME( MONTH,(Submission.SubmissionDate)) AS [Date]        
FROM Submission 
GROUP BY MONTH(Submission.SubmissionDate), DATENAME( MONTH,(Submission.SubmissionDate))
ORDER BY MONTH(Submission.SubmissionDate)

这也适用于您的真实查询。

答案 2 :(得分:0)

您可以使用MIN()MAX()

SELECT  DATENAME(MONTH,(Submission.SubmissionDate)) AS [Date]        
FROM Submission 
GROUP BY DATENAME(MONTH,(Submission.SubmissionDate))
ORDER BY MIN([Date]) ASC;

这会从每个组中选择一个任意日期,然后按顺序排序。

顺便说一句,你可能应该关心年份和月份。如果是这样的话:

SELECT YEAR(s.SubmissionDate), DATENAME(MONTH, s.SubmissionDate) AS [Date]        
FROM Submission s
GROUP BY YEAR(s.SubmissionDate), DATENAME(MONTH, s.SubmissionDate)
ORDER BY MIN([Date]) ASC;