按顺序在sql中按月和年排序

时间:2013-06-24 08:44:23

标签: sql sql-server

我有一个存储过程,select语句是:

SELECT     { fn MONTHNAME(OrderDate) } AS MonthName, YEAR(OrderDate) AS Year, SUM(TotalValue) AS Profits
FROM         [Order]
WHERE     (YEAR(OrderDate) = @year)
GROUP BY { fn MONTHNAME(OrderDate) }, YEAR(OrderDate)

这显示了每个月的总和 但我需要按月和年份订购结果,因为我的结果显示如下:

April 2013 
February 2013 
January 2013 
June 2013 
March 2013 
May 2013 

在这种情况下解决方案是什么?

7 个答案:

答案 0 :(得分:12)

试试这个:

SELECT     { fn MONTHNAME(OrderDate) } AS MonthName, YEAR(OrderDate) AS Year, SUM(TotalValue) AS Profits
FROM         [Order]
WHERE     (YEAR(OrderDate) = @year)
GROUP BY { fn MONTHNAME(OrderDate) }, MONTH(OrderDate), YEAR(OrderDate)
order by Year(orderDate),month(OrderDate)

请注意,您需要将您订购的任何字段添加到group by子句

答案 1 :(得分:1)

我认为你最后应该使用order by条款

SELECT     { fn MONTHNAME(OrderDate) } AS MonthName, YEAR(OrderDate) AS Year, SUM(TotalValue) AS Profits
FROM         [Order]
WHERE     (YEAR(OrderDate) = @year)
GROUP BY { fn MONTHNAME(OrderDate) }, YEAR(OrderDate) 
ORDER BY { fn MONTH(OrderDate) }, YEAR(OrderDate)

你可以这样做

SELECT     CASE { fn MONTH(OrderDate) } 
            when 0 then 'JAN'
            when 1 then 'FEB'
            when 2 then 'MAR'
            when 3 then 'APR'
            when 4 then 'MAY'
            when 5 then 'JUN'
            when 6 then 'JUL'
            when 7 then 'AUG'
            when 8 then 'SEP'
            when 9 then 'OCT'
            when 10 then 'NOV'
            when 11 then 'DEC'
           END
      AS MonthName, YEAR(OrderDate) AS Year, SUM(TotalValue) AS Profits
    FROM         [Order]
    WHERE     (YEAR(OrderDate) = @year)
    GROUP BY { fn MONTH(OrderDate) }, YEAR(OrderDate) 
    ORDER BY { fn MONTH(OrderDate) }, YEAR(OrderDate)

答案 2 :(得分:1)

如果您想订购1月,2月,3月使用

SELECT 
  month(OrderDate) MonthName, 
  year(OrderDate) Year, 
  sum(TotalValue) Profits
FROM         
  [Order]
WHERE     
  month(OrderDate) = @year
ORDER BY 
  year(OrderDate), 
  month(OrderDate)
GROUP BY 
  year(OrderDate),
  month(OrderDate)

答案 3 :(得分:1)

添加

Order by max(OrderDate) 

最后。

SELECT     { fn MONTHNAME(OrderDate) } AS MonthName, YEAR(OrderDate) AS Year,     SUM(TotalValue) AS Profits
FROM         [Order]
WHERE     (YEAR(OrderDate) = @year)
GROUP BY { fn MONTHNAME(OrderDate) }, YEAR(OrderDate)
Order by max(OrderDate) 

现在关于它如何运作:

如果按月,年分别订购,则按字母顺序(1月1月前)按月升序排列。如果您按订单日期订购,则将根据日期值订购ID,该日期值当然按月/年排序。

答案 4 :(得分:0)

使用以下查询它将适合您。

SELECT     { fn MONTHNAME(OrderDate) } AS MonthName, datepart(MM,OrderDate)                     AS Month, YEAR(OrderDate) AS Year, SUM(TotalValue) AS Profits
FROM         [Order]
WHERE     (YEAR(OrderDate) = @year)
GROUP BY { fn MONTHNAME(OrderDate) }, YEAR(OrderDate),     datepart(MM,**OrderDate**) 
ORDER BY datepart(MM,**OrderDate**)

答案 5 :(得分:0)

select 
cast(year(appointmentdate) as char(4))+''+ 
case when len(cast(month(appointmentdate)as char(2)))=1 
then '0'+cast(  month(appointmentdate) as char(2) ) else cast(month(appointmentdate) as char(2))end  as apporder
from timeslots  
group by appointmentdate
order by appOrder

答案 6 :(得分:0)

试试这个。它完美地运作。请注意,您已输入年份值。因此,您不需要订购或分组年份,因为您一次只能选择一年。因此不需要额外的代码。

SELECT { fn MONTHNAME(OrderDate) } AS MonthName, SUM(TotalValue) AS Profits
FROM [Order]
WHERE (YEAR(OrderDate) = @year)
GROUP BY { fn MONTHNAME(OrderDate) }, DATEPART(M, OrderDate)
ORDER BY DATEPART(M, OrderDate)