1

如何创建存储过程,以提供按月和年分组的总计

销售表名

year week  date      value
2008 4    2008-01-21  50
2008 5    2008-02-22  25
2008 6    2008-02-23  30
4

4 回答 4

0

可以使用简单的 SQL 来完成:

SELECT to_char(date,'MON-YYYY'), SUM(VALUE) FROM SALE
GROUP BY to_char(date,'MON-YYYY');
于 2013-07-02T06:28:31.460 回答
0

试试这个

SELECT year,MONTH(date) AS [Month], SUM(value) AS [value] FROM sale 
GROUP BY year,MONTH(date)
于 2013-07-02T06:33:55.787 回答
0

这是一个完整的存储过程代码,带有参数和如何运行它的示例。

CREATE PROCEDURE dbo.GetTotalByDate
(
    @StartDate datetime,
    @EndDate datetime
)
AS 
BEGIN
    SELECT S.year as [Year],
    MONTH(S.date) as [Month],
    SUM(S.Value) as [Value]
    FROM Sale S
    WHERE S.date BETWEEN @StartDate and @EndDate
    GROUP BY S.year, MONTH(S.date) 
    ORDER BY [Year], [Month] 
END


EXEC dbo.GetTotalByDate @StartDate = '01/01/2013', @EndDate = '01/01/2014'
于 2013-07-02T09:25:21.847 回答
0

试试这个

CREATE PROCEDURE dbo.GetTotalAmount
AS 
BEGIN
    SELECT DATEPART(YEAR, date)as [Year],
    MONTH(date) as [Month],
    SUM(Value) as [Value]
    FROM Sale 
    GROUP BY DATEPART(YEAR, date), MONTH(date) 
END

EXEC dbo.GetTotalAmount
于 2013-07-02T09:28:35.927 回答