Netezza:显示日期,即使当天的数据为 0

Netezza: Show dates even if 0 data for that day

我通过 excel 中的 odbc 连接进行此查询,以获取包含每 4 周数据的可刷新报告。即使那天没有数据,我也需要显示 4 周中每一周的日期,因为这些数据随后会链接到图表。有办法吗?

谢谢。

Select  b.INV_DT, sum( a.ORD_QTY) as Ordered, sum( a.SHIPPED_QTY) as Shipped
    from fct_dly_invoice_detail a, fct_dly_invoice_header b, dim_invoice_customer c
    where a.INV_HDR_SK = b.INV_HDR_SK
    and b.DIM_INV_CUST_SK = c.DIM_INV_CUST_SK
    and a.SRC_SYS_CD = 'ABC'
    and a.NDC_NBR is not null 
    **and b.inv_dt between CURRENT_DATE - 16 and CURRENT_DATE**
    and b.store_nbr in (2851, 2963, 3249, 3385, 3447, 3591, 3727, 4065, 4102, 4289, 4376, 4793, 5209, 5266, 5312, 5453, 5569, 5575, 5892, 6534, 6571, 7110, 9057, 9262, 9652, 9742, 10373, 12392, 12739, 13870
    )
    group by 1

对此的通用解决方案是创建一个日期维度 table,然后在 INV_DT 列上对该日期维度 table 执行外部联接。

您可以搜索大量关于创建良好日期维度的好资源 table,因此我将在此处创建一个快速而粗略(且微不足道)的示例。如果您要进行大量 BI/reporting.

,我强烈建议您在该领域进行一些研究

如果我们的 table 我们想要报告的来源如下所示:

                Table "TABLEZ"
 Attribute |  Type  | Modifier | Default Value
-----------+--------+----------+---------------
 AMOUNT    | BIGINT |          |
 INV_DT    | DATE   |          |
Distributed on random: (round-robin)

select * from tablez order by inv_dt

 AMOUNT |   INV_DT
--------+------------
      1 | 2015-04-04
      1 | 2015-04-04
      1 | 2015-04-06
      1 | 2015-04-06
(4 rows)

我们的报告如下所示:

SELECT inv_dt,
   SUM(amount)
FROM tablez
WHERE inv_dt BETWEEN CURRENT_DATE - 5 AND CURRENT_DATE
GROUP BY inv_dt;

   INV_DT   | SUM
------------+-----
 2015-04-04 |   2
 2015-04-06 |   2
(2 rows)

我们可以创建一个日期维度 table,其中包含每个日期的一行(或者在本例中使用 _v_vector_idx 视图,在过去 1024 天和未来 1024 天吃掉) .

create table date_dim (date_dt date);

insert into date_dim select current_date - idx from _v_vector_idx;
insert into date_dim select current_date + idx +1 from _v_vector_idx;

那么我们的查询将如下所示:

SELECT d.date_dt,
   SUM(amount)
FROM tablez a
   RIGHT OUTER JOIN date_dim d
   ON a.inv_dt = d.date_dt
WHERE d.date_dt BETWEEN CURRENT_DATE -5 AND CURRENT_DATE
GROUP BY d.date_dt;


  DATE_DT   | SUM
------------+-----
 2015-04-01 |
 2015-04-02 |
 2015-04-03 |
 2015-04-04 |   2
 2015-04-05 |
 2015-04-06 |   2
(6 rows)

如果您在没有数据的日子里确实需要一个零值而不是 NULL,您可以像这样使用 COALESCE 或 NVL:

SELECT d.date_dt,
   COALESCE(SUM(amount),0)
FROM tablez a
   RIGHT OUTER JOIN date_dim d
   ON a.inv_dt = d.date_dt
WHERE d.date_dt BETWEEN CURRENT_DATE -5 AND CURRENT_DATE
GROUP BY d.date_dt;


  DATE_DT   | COALESCE
------------+----------
 2015-04-01 |        0
 2015-04-02 |        0
 2015-04-03 |        0
 2015-04-04 |        2
 2015-04-05 |        0
 2015-04-06 |        2
(6 rows)

我同意@ScottMcG 的观点,您需要获取日期列表。但是,如果您处于不允许创建 table 的情况。你可以简化事情。您只需要一个至少有 28 行的 table。使用您的示例,这应该可行。

select date_list.dt_nm, nvl(results.Ordered,0) as Ordered, nvl(results.Shipped,0) as Shipped
from
(select row_number() over(order by sub.arb_nbr)+ (current_date -28) as dt_nm
from (select rowid as arb_nbr
from fct_dly_invoice_detail b
limit 28) sub ) date_list left outer join


( Select  b.INV_DT, sum( a.ORD_QTY) as Ordered, sum( a.SHIPPED_QTY) as Shipped
    from fct_dly_invoice_detail a inner join
     fct_dly_invoice_header b
         on     a.INV_HDR_SK = b.INV_HDR_SK
                and a.SRC_SYS_CD = 'ABC'
                and a.NDC_NBR is not null
                 **and b.inv_dt between CURRENT_DATE - 16 and CURRENT_DATE**
                     and b.store_nbr in (2851, 2963, 3249, 3385, 3447, 3591, 3727, 4065, 4102, 4289, 4376, 4793, 5209, 5266, 5312, 5453, 5569, 5575, 5892, 6534, 6571, 7110, 9057, 9262, 9652, 9742, 10373, 12392, 12739, 13870)
         inner join
     dim_invoice_customer c
    on b.DIM_INV_CUST_SK = c.DIM_INV_CUST_SK
    group by 1 ) results

on date_list.dt_nm = results.inv_dt