每月总销售额

时间:2011-05-12 21:32:30

标签: sql-server tsql

我认为这很常见,是吗?

如果您的表格为SALES {Customer_ID, Price, SmallDateTime date}。您如何报告每月的所有销售额?

SELECT Sum(Price) As Total Sales FROM SALES Group By What Having What 

由于

4 个答案:

答案 0 :(得分:26)

  SELECT YEAR(date) as SalesYear,
         MONTH(date) as SalesMonth,
         SUM(Price) AS TotalSales
    FROM Sales
GROUP BY YEAR(date), MONTH(date)
ORDER BY YEAR(date), MONTH(date)

答案 1 :(得分:1)

SELECT CONVERT(CHAR(7), SmallDateTime, 120) as Year_Month,
       SUM(Price)
    FROM Sales
    GROUP BY CONVERT(CHAR(7), SmallDateTime, 120) 
    ORDER BY Year_Month

答案 2 :(得分:0)

另一个解决方案是计算一个月的第一天

Select DateAdd(d,DateDiff(d,0,[Date])-DatePart(d,[Date])+1,0)
    , Sum( Price )
From Sales
Group By DateAdd(d,DateDiff(d,0,[Date])-DatePart(d,[Date])+1,0)

答案 3 :(得分:0)

试试这个

SELECT A.CAL_YEAR AS YEAR  ,A.CAL_WEEK AS WEEK,SUM(B.SUM_OF_PROFIT) as profit FROM
CALANDER A,SALES_FACT B WHERE A.DATE_ID=B.DATE_ID
GROUP BY A.CAL_YEAR,A.CAL_WEEK;