Fill In The Date Gaps With Date Table

前端 未结 4 1997
甜味超标
甜味超标 2020-12-21 04:47

I have two tables.

An orders table with customer, and date. A date dimension table from a data warehouse.

The orders table does not contain activity for ev

4条回答
  •  猫巷女王i
    2020-12-21 05:10

    Here is a simple way to do it:

    SELECT  A.Customer,
            B.fulldate [Date],
            ISNULL(C.Amount,0) Amount
    FROM (  SELECT  Customer, 
                    MIN([Date]) MinDate,
                    MAX([Date]) MaxDate
            FROM Orders
            GROUP BY Customer) A
    LEFT JOIN DateTable B
        ON B.fulldate BETWEEN A.MinDate AND A.MaxDate
    LEFT JOIN Orders C
        ON A.Customer = C.Customer 
        AND B.fulldate = C.[Date]
    

提交回复
热议问题