Fill In The Date Gaps With Date Table

前端 未结 4 1999
甜味超标
甜味超标 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条回答
  •  孤城傲影
    2020-12-21 05:20

    The problem is that you need all customers for all dates. When you do the left outer join, you are getting NULL for the customer field.

    The following sets up a driver table by cross joining the customer names and dates:

    SELECT driver.customer, driver.fulldate, o.amount 
    FROM   (select d.fulldate, customer
            from datetable d cross join
                 (select customer
                  from orders
                  where year(orderdate) in (2012)
                 ) o
            where d.calendaryear IN ( 2012 )
           ) driver LEFT OUTER JOIN
           orders o 
           ON driver.fulldate = o.orderdate and
              driver.customer = o.customer;
    

    Note that this version assumes that calendaryear is the same as year(orderdate).

提交回复
热议问题