Summarise by week, even for empty rows

后端 未结 4 663
旧时难觅i
旧时难觅i 2021-01-21 23:14

I want to summarise the sum of sales.quantity by week, and to show the week number even if there are no sales.

I have setup a weeks table with 1-54 in there to use an ou

4条回答
  •  臣服心动
    2021-01-21 23:30

    As @msmucker0527 wrote get rid of WHERE (sales.transDate BETWEEN @fromDate AND @toDate). You can do it also this way:

    SELECT Weeks.WeekNum, SUM(sales.quantity) AS sales
    FROM Weeks W  
         LEFT JOIN sales S ON W.WeekNum = DATEPART(week, S.transDate)
                AND S.transDate BETWEEN @fromDate AND @toDate)
    GROUP BY W.WeekNum
    

    Also, this WHERE (sales.transDate BETWEEN @fromDate AND @toDate) guarantee the Index Scan of Sales table which can greatly slow down your query.
    You'd better include columns WeekFirstDate datetime and WeekLastDate datetime into Weeks table and CREATE NONCLUSTERED INDEX IX_Name ON Sales (TransDate) INCLUDE (quantity). In this case your query can be changed this way:

    SELECT Weeks.WeekNum, SUM(sales.quantity) AS sales
    FROM Weeks W  
         LEFT JOIN sales S ON S.transDate>=W.WeekFirstDate
                              AND S.transDate<=W.WeekLastDate
                              AND S.transDate BETWEEN @fromDate AND @toDate)
    GROUP BY W.WeekNum
    

提交回复
热议问题