如何在Oracle中使数据透视表具有默认值?

时间:2019-04-24 11:02:06

标签: sql oracle pivot

这个问题可能与我的other question有关。

我有一个查询,它以QTDEATR为中心,而且以日期为中心,所以我做了以下

SELECT      *
FROM (
    SELECT  ...
            GPC.EVENT_VALUE,
            GPC.IDENTIFIER,
            TO_CHAR(TO_DATE(GPC.SCHEDULE_DATE), 'MM') AS SCHEDULE_DATE
    FROM    GP_CALCULATION GPC
    WHERE   RO_CODE = 'MQ'
    AND     GPC.IDENTIFIER IN ('QTDE', 'ATR')
) 
PIVOT(   
    SUM(EVENT_VALUE)
    FOR (IDENTIFIER, SCHEDULE_DATE)
    IN (
        ('QTDE', '01') AS QTDE01,
        ('ATR', '01') AS ATR01,
        ('QTDE', '02') AS QTDE02,
        ('ATR', '02') AS ATR02,
        ('QTDE', '03') AS QTDE03,
        ('ATR', '03') AS ATR03,
        ('QTDE', '04') AS QTDE04,
        ('ATR', '04') AS ATR04,
        ('QTDE', '05') AS QTDE05,
        ('ATR', '05') AS ATR05,
        ('QTDE', '06') AS QTDE06,
        ('ATR', '06') AS ATR06,
        ('QTDE', '07') AS QTDE07,
        ('ATR', '07') AS ATR07,
        ('QTDE', '08') AS QTDE08,
        ('ATR', '08') AS ATR08,
        ('QTDE', '09') AS QTDE09,
        ('ATR', '09') AS ATR09,
        ('QTDE', '10') AS QTDE10,
        ('ATR', '10') AS ATR10,
        ('QTDE', '11') AS QTDE11,
        ('ATR', '11') AS ATR11,
        ('QTDE', '12') AS QTDE12,
        ('ATR', '12') AS ATR12
    )
)         

问题

当枢轴上没有匹配项时,它就是这样

query

我正在使用PL / SQL Developer来查看结果,但出现了“空白”,我不知道这是什么。那等于''吗?

如何使“空白”的值变成0

1 个答案:

答案 0 :(得分:1)

这意味着对于该标识符/月,表中没有数据(或者只有空值)。如果您想查看0,请在case部分使用nvlselect,例如以下示例:

-- sample data 
with gp_calculation(event_value, identifier, schedule_date, ro_code) as (
    select 12, 'QTDE', date '2018-12-24', 'MQ' from dual union all
    select  7, 'QTDE', date '2018-12-24', 'MQ' from dual union all
    select 42, 'ATR',  date '2018-11-24', 'MQ' from dual union all
    select 13, 'QTDE', date '2018-11-24', 'MQ' from dual union all
    select 17, 'QTDE', date '2018-11-24', 'MQ' from dual )
-- end of data

select nvl(q10, 0) qtde10, nvl(a10, 0) atr10, 
       nvl(q11, 0) qtde11, nvl(a11, 0) atr11, 
       nvl(q12, 0) qtde12, nvl(a12, 0) atr12
  from (select  gpc.event_value, gpc.identifier,
                to_char(to_date(gpc.schedule_date), 'MM') as schedule_date
          from  gp_calculation gpc
          where ro_code = 'MQ'
            and gpc.identifier in ('QTDE', 'ATR')) 
  pivot(sum(event_value) for (identifier, schedule_date) in (
                               ('QTDE', '10') as q10, ('ATR', '10') as a10,
                               ('QTDE', '11') as q11, ('ATR', '11') as a11,
                               ('QTDE', '12') as q12, ('ATR', '12') as a12 ))

结果:

    QTDE10      ATR10     QTDE11      ATR11     QTDE12      ATR12
---------- ---------- ---------- ---------- ---------- ----------
         0          0         30         42         19          0