これは私が使用しているテーブルのddlです。複数の列、複数のテーブルの列とピボットのない行
CREATE TABLE "BISAMPLE"."SUMLEDGER"
( "FUND" VARCHAR2(6 BYTE),
"ORG" VARCHAR2(6 BYTE),
"ACCT" VARCHAR2(6 BYTE),
"PROG" VARCHAR2(6 BYTE),
"YEAR" NUMBER,
"S01_BUDGET" NUMBER,
"S01_BUDGET_ADJ" NUMBER,
"S01_YTD_ACTV" NUMBER,
"S01_ENCUMB" NUMBER,
"S02_BUDGET" NUMBER,
"S02_BUDGET_ADJ" NUMBER,
"S02_YTD_ACTV" NUMBER,
"S02_ENCUMB" NUMBER
)
ここにサンプルデータが挿入されています。
REM INSERTING into SUMLEDGER
SET DEFINE OFF;
Insert into SUMLEDGER (FUND,ORG,ACCT,PROG,YEAR,S01_BUDGET,S01_BUDGET_ADJ,S01_YTD_ACTV,S01_ENCUMB,S02_BUDGET,S02_BUDGET_ADJ,S02_YTD_ACTV,S02_ENCUMB) values ('100001','3999','3220','06',6,20,30,15,15,10,20,10,40);
Insert into SUMLEDGER (FUND,ORG,ACCT,PROG,YEAR,S01_BUDGET,S01_BUDGET_ADJ,S01_YTD_ACTV,S01_ENCUMB,S02_BUDGET,S02_BUDGET_ADJ,S02_YTD_ACTV,S02_ENCUMB) values ('100001','3999','3220','06',7,20,30,15,15,10,20,30,40);
Insert into SUMLEDGER (FUND,ORG,ACCT,PROG,YEAR,S01_BUDGET,S01_BUDGET_ADJ,S01_YTD_ACTV,S01_ENCUMB,S02_BUDGET,S02_BUDGET_ADJ,S02_YTD_ACTV,S02_ENCUMB) values ('100001','3999','3460','06',6,25,30,20,20,20,20,30,40);
Insert into SUMLEDGER (FUND,ORG,ACCT,PROG,YEAR,S01_BUDGET,S01_BUDGET_ADJ,S01_YTD_ACTV,S01_ENCUMB,S02_BUDGET,S02_BUDGET_ADJ,S02_YTD_ACTV,S02_ENCUMB) values ('100001','3999','3220','06',8,20,35,25,30,30,22,15,30);
これは、このクエリで使用する2番目のテーブルです。
CREATE TABLE "BISAMPLE"."FUNDING"
( "FUNDING_START_DATE" DATE,
"FUNDING_NUMBER" VARCHAR2(6 BYTE)
)
ここには表の挿入があります。
REM INSERTING into FUNDING
SET DEFINE OFF;
Insert into FUNDING (FUNDING_START_DATE,FUNDING_NUMBER) values (to_date('21-JAN-2017 03:47:42','DD-MON-RRRR HH24:MI:SS'),'100001');
私は列を行に変換するためにunpivot関数を使用しようとしています。しかし、私は現時点ではかなり失敗しています。しかし、私は次のクエリを使用して同じ出力を実現することができます。
SELECT
FUND,
ORG,
ACCT,
PROG,
YEAR,
SUM(S01_BUDGET + S01_BUDGET_ADJ) BUDG,
S01_YTD_ACTV YEAR_TO_DATE,
S01_ENCUMB ENCUMBRANCE,
'01' AS SPERIOD,
CASE
--PERIOD 01
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JAN'
THEN '01' --JAN PERIOD 01
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'FEB'
THEN '02' --FEB
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAR'
THEN '03' --MAR
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'APR'
THEN '04'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAY'
THEN '05'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUN'
THEN '06'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUL'
THEN '07'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'AUG'
THEN '08' --AUG
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'SEP'
THEN '09'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'OCT'
THEN '10' --OCT
WHEN SUBSTR(TO_CHAR(FUNDING_NUMBER,'MON-YYYY'),1,3) = 'NOV'
THEN '11'
WHEN SUBSTR(TO_CHAR(FUNDING_NUMBER,'MON-YYYY'),1,3) = 'DEC'
THEN '12'
ELSE '00'
END AS SMONTH,
(CASE
--PERIOD 01
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JAN'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'FEB'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAR'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'APR'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAY'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUN'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUL'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'AUG'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'SEP'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'OCT'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'NOV'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'DEC'
THEN TO_NUMBER(YEAR)
ELSE TO_NUMBER(YEAR)
END) AS S_YEAR
FROM SUMLEDGER ,
FUNDING
WHERE FUNDING_NUMBER = FUND
group by
FUND,
ORG,
ACCT,
PROG,
YEAR,
S01_YTD_ACTV ,
S01_ENCUMB,
'01' ,
CASE
--PERIOD 01
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JAN'
THEN '01' --JAN PERIOD 01
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'FEB'
THEN '02' --FEB
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAR'
THEN '03' --MAR
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'APR'
THEN '04'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAY'
THEN '05'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUN'
THEN '06'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUL'
THEN '07'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'AUG'
THEN '08' --AUG
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'SEP'
THEN '09'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'OCT'
THEN '10' --OCT
WHEN SUBSTR(TO_CHAR(FUNDING_NUMBER,'MON-YYYY'),1,3) = 'NOV'
THEN '11'
WHEN SUBSTR(TO_CHAR(FUNDING_NUMBER,'MON-YYYY'),1,3) = 'DEC'
THEN '12'
ELSE '00'
END ,
(CASE
--PERIOD 01
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JAN'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'FEB'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAR'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'APR'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAY'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUN'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUL'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'AUG'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'SEP'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'OCT'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'NOV'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'DEC'
THEN TO_NUMBER(YEAR)
ELSE TO_NUMBER(YEAR)
END)
UNION ALL
SELECT
FUND,
ORG,
ACCT,
PROG,
YEAR,
SUM(S02_BUDGET + S02_BUDGET_ADJ) BUDG,
S02_YTD_ACTV YEAR_TO_DATE,
S02_ENCUMB ENCUMBRANCE,
'01' AS SPERIOD,
CASE
--PERIOD 01
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JAN'
THEN '02' --JAN PERIOD 01
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'FEB'
THEN '03' --FEB
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAR'
THEN '04' --MAR
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'APR'
THEN '05'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAY'
THEN '06'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUN'
THEN '07'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUL'
THEN '08'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'AUG'
THEN '09' --AUG
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'SEP'
THEN '10'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'OCT'
THEN '11' --OCT
WHEN SUBSTR(TO_CHAR(FUNDING_NUMBER,'MON-YYYY'),1,3) = 'NOV'
THEN '12'
WHEN SUBSTR(TO_CHAR(FUNDING_NUMBER,'MON-YYYY'),1,3) = 'DEC'
THEN '01'
ELSE '00'
END AS SMONTH,
(CASE
--PERIOD 02
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JAN'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'FEB'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAR'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'APR'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAY'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUN'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUL'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'AUG'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'SEP'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'OCT'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'NOV'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'DEC'
THEN TO_NUMBER(YEAR)+1
ELSE TO_NUMBER(YEAR)
END) AS S_YEAR
FROM SUMLEDGER ,
FUNDING
WHERE FUNDING_NUMBER = FUND
group by
FUND,
ORG,
ACCT,
PROG,
YEAR,
S02_YTD_ACTV ,
S02_ENCUMB ,
'01' ,
CASE
--PERIOD 01
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JAN'
THEN '02' --JAN PERIOD 01
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'FEB'
THEN '03' --FEB
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAR'
THEN '04' --MAR
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'APR'
THEN '05'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAY'
THEN '06'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUN'
THEN '07'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUL'
THEN '08'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'AUG'
THEN '09' --AUG
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'SEP'
THEN '10'
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'OCT'
THEN '11' --OCT
WHEN SUBSTR(TO_CHAR(FUNDING_NUMBER,'MON-YYYY'),1,3) = 'NOV'
THEN '12'
WHEN SUBSTR(TO_CHAR(FUNDING_NUMBER,'MON-YYYY'),1,3) = 'DEC'
THEN '01'
ELSE '00'
END ,
(CASE
--PERIOD 02
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JAN'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'FEB'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAR'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'APR'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'MAY'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUN'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'JUL'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'AUG'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'SEP'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'OCT'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'NOV'
THEN TO_NUMBER(YEAR)
WHEN SUBSTR(TO_CHAR(FUNDING_START_DATE,'MON-YYYY'),1,3) = 'DEC'
THEN TO_NUMBER(YEAR)+1
ELSE TO_NUMBER(YEAR)
END)
誰かがピボット機能を使用してこれを書き直すのを助けることができますか?上記の方法を使用すると、14の可能な期間/列が存在するため、少なくとも14回の結合が必要になります。この表には約98個の列が含まれています。ここでは例を減らして説明しやすく、圧倒しません。ここで
は、私が期待した結果に
"FUND" "ORG" "ACCT" "PROG" "YEAR" "BUDG" "YEAR_TO_DATE" "ENCUMBRANCE" "SPERIOD" "SMONTH" "S_YEAR"
"100001" "3999" "3220" "06" 6 50 15 15 "01" "01" 6
"100001" "3999" "3220" "06" 7 50 15 15 "01" "01" 7
"100001" "3999" "3220" "06" 8 55 25 30 "01" "01" 8
"100001" "3999" "3460" "06" 6 55 20 20 "01" "01" 6
"100001" "3999" "3220" "06" 6 30 10 40 "01" "02" 6
"100001" "3999" "3220" "06" 8 52 15 30 "01" "02" 8
"100001" "3999" "3460" "06" 6 40 30 40 "01" "02" 6
"100001" "3999" "3220" "06" 7 30 30 40 "01" "02" 7
はまだここで何が起こっているか理解しようと、私は質問を理解することができますし、私はそれに答えることができれば(私は回答を追加します)。しかし、 'substr(to_char(some_date、 'MON-YYYY')、1,3)'は、 'to_char(some_date、 'MON')'のように短時間で効率的に書くことができます。 'CASE'ステートメントは次のようにコンパクトに書くことができます:' CASE TO_CHAR(some_date、 'MON') 'JAN' THEN ... 'FEB' THEN ... '式を12回繰り返す。 – mathguy