在Create table中选择group by的问题

时间:2018-05-09 15:22:07

标签: oracle

我对Oracle的查询有一个非常奇怪的问题。如果我是否根据此查询的结果创建表格,那么我对选择查询的结果不会相同。

1 /仅选择查询:

SELECT PRJ_OBJECT_ID,SUBSTR(YEAR,1,4) YEAR,'ETC' UNIT,
    SUM(JAN) JAN,SUM(FEB) FEB,SUM(MAR) MAR,SUM(APR) APR,SUM(MAY) MAY,SUM(JUN) JUN,
    SUM(JUL) JUL,SUM(AUG) AUG,SUM(SEP) SEP,SUM(OCTO) OCT,SUM(NOV) NOV,SUM(DECE) DECE,sum(total) total
FROM(
    SELECT PRJ_OBJECT_ID,TO_CHAR(SLICE_DATE,'YYYYMM') YEAR,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('01'),SUM(SLICE),0) JAN,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('02'),SUM(SLICE),0) FEB,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('03'),SUM(SLICE),0) MAR,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('04'),SUM(SLICE),0) APR,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('05'),SUM(SLICE),0) MAY,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('06'),SUM(SLICE),0) JUN,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('07'),SUM(SLICE),0) JUL,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('08'),SUM(SLICE),0) AUG,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('09'),SUM(SLICE),0) SEP,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('10'),SUM(SLICE),0) OCTO,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('11'),SUM(SLICE),0) NOV,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('12'),SUM(SLICE),0) DECE,
            NVL(SUM(SLICE),0) TOTAL
        FROM PRJ_BLB_SLICES_M_ETC INNER JOIN PRASSIGNMENT PA ON PA.PRID=PRJ_OBJECT_ID
        WHERE SLICE_REQUEST_ID=113
        AND  TO_CHAR(SLICE_DATE, 'YYYY') >= TO_CHAR(ADD_MONTHS(SYSDATE,-3), 'YYYY')
  and PRJ_OBJECT_ID = 5002239
    group by PRJ_OBJECT_ID,TO_CHAR(SLICE_DATE,'YYYYMM')
    )
group by PRJ_OBJECT_ID,SUBSTR(YEAR,1,4)

结果:我只有一行 Result

2 /从结果

创建表格
CREATE TABLE "U_PR4_1"."MI_ETC_TEMP" as (
SELECT PRJ_OBJECT_ID,SUBSTR(YEAR,1,4) YEAR,'ETC' UNIT,
    SUM(JAN) JAN,SUM(FEB) FEB,SUM(MAR) MAR,SUM(APR) APR,SUM(MAY) MAY,SUM(JUN) JUN,
    SUM(JUL) JUL,SUM(AUG) AUG,SUM(SEP) SEP,SUM(OCTO) OCT,SUM(NOV) NOV,SUM(DECE) DECE,sum(total) total
FROM(
    SELECT PRJ_OBJECT_ID,TO_CHAR(SLICE_DATE,'YYYYMM') YEAR,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('01'),SUM(SLICE),0) JAN,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('02'),SUM(SLICE),0) FEB,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('03'),SUM(SLICE),0) MAR,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('04'),SUM(SLICE),0) APR,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('05'),SUM(SLICE),0) MAY,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('06'),SUM(SLICE),0) JUN,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('07'),SUM(SLICE),0) JUL,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('08'),SUM(SLICE),0) AUG,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('09'),SUM(SLICE),0) SEP,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('10'),SUM(SLICE),0) OCTO,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('11'),SUM(SLICE),0) NOV,
            DECODE(TO_CHAR(SLICE_DATE,'YYYYMM'),SUBSTR(TO_CHAR(SLICE_DATE,'YYYYMM'),1,4)||TO_CHAR('12'),SUM(SLICE),0) DECE,
            NVL(SUM(SLICE),0) TOTAL
        FROM PRJ_BLB_SLICES_M_ETC INNER JOIN PRASSIGNMENT PA ON PA.PRID=PRJ_OBJECT_ID
        WHERE SLICE_REQUEST_ID=113
        AND  TO_CHAR(SLICE_DATE, 'YYYY') >= TO_CHAR(ADD_MONTHS(SYSDATE,-3), 'YYYY')
  and PRJ_OBJECT_ID = 5002239
    group by PRJ_OBJECT_ID,TO_CHAR(SLICE_DATE,'YYYYMM')
    )
group by PRJ_OBJECT_ID,SUBSTR(YEAR,1,4))

表中的结果:一个月一行

Result table 感谢您的帮助,

1 个答案:

答案 0 :(得分:0)

通过更新查询(在另一个论坛中提供的解决方案)修复了

问题:

  select PR.PRJ_OBJECT_ID
   , to_char(SLICE_DATE, 'YYYY')                                         as YEAR
   , 'ETC'                                                               as UNIT
   , sum(case to_char(SLICE_DATE, 'MM') when '01' then SLICE else 0 end) as JAN
   , sum(case to_char(SLICE_DATE, 'MM') when '02' then SLICE else 0 end) as FEB
   , sum(case to_char(SLICE_DATE, 'MM') when '03' then SLICE else 0 end) as MAR
   , sum(case to_char(SLICE_DATE, 'MM') when '04' then SLICE else 0 end) as APR
   , sum(case to_char(SLICE_DATE, 'MM') when '05' then SLICE else 0 end) as MAY
   , sum(case to_char(SLICE_DATE, 'MM') when '06' then SLICE else 0 end) as JUN
   , sum(case to_char(SLICE_DATE, 'MM') when '07' then SLICE else 0 end) as JUL
   , sum(case to_char(SLICE_DATE, 'MM') when '08' then SLICE else 0 end) as AUG
   , sum(case to_char(SLICE_DATE, 'MM') when '09' then SLICE else 0 end) as SEP
   , sum(case to_char(SLICE_DATE, 'MM') when '10' then SLICE else 0 end) as OCTO
   , sum(case to_char(SLICE_DATE, 'MM') when '11' then SLICE else 0 end) as NOV
   , sum(case to_char(SLICE_DATE, 'MM') when '12' then SLICE else 0 end) as DECE
   , coalesce(sum(SLICE), 0)                                             as TOTAL
from PRJ_BLB_SLICES_M_ETC PR
join PRASSIGNMENT         PA ON PA.PRID = PR.PRJ_OBJECT_ID  where PR.SLICE_REQUEST_ID = 113
 and SLICE_DATE         >= trunc(add_months(sysdate, -3), 'yyyy')
 and PR.PRJ_OBJECT_ID    = 5002239 group by PR.PRJ_OBJECT_ID
   , to_char(SLICE_DATE, 'YYYY'); 

问题似乎是Oracle 12中的一个错误。