Total sales per month

前端 未结 4 514
时光取名叫无心
时光取名叫无心 2021-02-05 10:20

I think this is common, is it?

If you have a table with SALES {Customer_ID, Price, SmallDateTime date}. How do you report all sales per month?



        
相关标签:
4条回答
  • 2021-02-05 10:54
      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)
    
    0 讨论(0)
  • 2021-02-05 11:03
    SELECT CONVERT(CHAR(7), SmallDateTime, 120) as Year_Month,
           SUM(Price)
        FROM Sales
        GROUP BY CONVERT(CHAR(7), SmallDateTime, 120) 
        ORDER BY Year_Month
    
    0 讨论(0)
  • 2021-02-05 11:08

    Try this

    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;
    
    0 讨论(0)
  • 2021-02-05 11:14

    Another solution is to calculate the first day of the month

    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)
    
    0 讨论(0)
提交回复
热议问题