這是一個強力的方式來實現,我以後什麼,但有可能是一個更有效的方式提高Oracle查詢的申請每月因素,每月預算的查詢值
SELECT sum(jan/(SELECT JAN FROM MONTHLY_FACTORS WHERE YEAR = '2017')) as FACTORED_JAN,
sum(feb/(SELECT FEB FROM MONTHLY_FACTORS WHERE YEAR = '2017')) as FACTORED_FEB,
sum(mar/(SELECT MAR FROM MONTHLY_FACTORS WHERE YEAR = '2017')) as FACTORED_MAR,
sum(apr/(SELECT APR FROM MONTHLY_FACTORS WHERE YEAR = '2017')) as FACTORED_APR,
sum(may/(SELECT MAY FROM MONTHLY_FACTORS WHERE YEAR = '2017')) as FACTORED_MAY,
sum(jun/(SELECT JUN FROM MONTHLY_FACTORS WHERE YEAR = '2017')) as FACTORED_JUN,
sum(jul/(SELECT JUL FROM MONTHLY_FACTORS WHERE YEAR = '2017')) as FACTORED_JUL,
sum(aug/(SELECT AUG FROM MONTHLY_FACTORS WHERE YEAR = '2017')) as FACTORED_AUG,
sum(sep/(SELECT SEP FROM MONTHLY_FACTORS WHERE YEAR = '2017')) as FACTORED_SEP,
sum(oct/(SELECT OCT FROM MONTHLY_FACTORS WHERE YEAR = '2017')) as FACTORED_OCT,
sum(nov/(SELECT NOV FROM MONTHLY_FACTORS WHERE YEAR = '2017')) as FACTORED_NOV,
sum(dec/(SELECT DEC FROM MONTHLY_FACTORS WHERE YEAR = '2017')) as FACTORED_DEC,
sum(
jan/(SELECT JAN FROM MONTHLY_FACTORS WHERE YEAR = '2017') +
feb/(SELECT FEB FROM MONTHLY_FACTORS WHERE YEAR = '2017') +
mar/(SELECT MAR FROM MONTHLY_FACTORS WHERE YEAR = '2017') +
apr/(SELECT APR FROM MONTHLY_FACTORS WHERE YEAR = '2017') +
may/(SELECT MAY FROM MONTHLY_FACTORS WHERE YEAR = '2017') +
jun/(SELECT JUN FROM MONTHLY_FACTORS WHERE YEAR = '2017') +
jul/(SELECT JUL FROM MONTHLY_FACTORS WHERE YEAR = '2017') +
aug/(SELECT AUG FROM MONTHLY_FACTORS WHERE YEAR = '2017') +
sep/(SELECT SEP FROM MONTHLY_FACTORS WHERE YEAR = '2017') +
oct/(SELECT OCT FROM MONTHLY_FACTORS WHERE YEAR = '2017') +
nov/(SELECT NOV FROM MONTHLY_FACTORS WHERE YEAR = '2017') +
dec/(SELECT DEC FROM MONTHLY_FACTORS WHERE YEAR = '2017')
) as FACTORED_YEAR_TOTAL
FROM MONTHLY_BUDGET
WHERE DEPT = '123ABC' AND YEAR = '2017'
的MONTHLY_FACTORS表看起來像
YEAR JAN FEB ... DEC
2016 3 2 ... 4
2017 ...
的MONTHLY_BUDGET表看起來像
YEAR DEPT JAN FEB ... DEC
2016 123abc 50234 20032 22234
2017 123abc ...
2016 xyz456 40054 23002 ... 44442
的預期輸出看起來像
YEAR JAN/JAN_FACTOR FEB/FEB_FACTOR ... DEC/DEC_FACTOR
有沒有一個更清潔的方式沒有所有的子查詢?
對於查看預期輸出的示例很有幫助。我在MONTHLY_BUDGET表的示例數據中沒有看到名爲DEPT的列。 – Stilgar
我應該沒有把部門編號弄混了。我應該把它排除在外。 – jeff
表格格式是固定的還是可以更改爲單行上的年份和月份? – Stilgar