проблема с группой в Создать таблицу как выберите - PullRequest
0 голосов
/ 09 мая 2018

У меня очень странная проблема с моим Запросом на 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 голосов
/ 14 мая 2018

проблема устранена путем обновления запроса (решение предоставлено на другом форуме):

  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.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...