從我的理解,01-OCT-2009 31-OCT-2009只有代碼X,ÿ和ž,而經歷到11月30日,2009年還可能會產生你瓦特 。
最後,你想這樣的查詢:
CREATE TABLE t1 AS
SELECT
abc.date,
MAX(DECODE(code, 'x', val_1, NULL)) AS abc_val_1_x,
MAX(DECODE(code, 'x', val_2, NULL)) AS abc_val_2_x,
MAX(DECODE(code, 'x', val_3, NULL)) AS abc_val_3_x,
MAX(DECODE(code, 'y', val_1, NULL)) AS abc_val_1_y,
MAX(DECODE(code, 'y', val_2, NULL)) AS abc_val_2_y,
MAX(DECODE(code, 'y', val_3, NULL)) AS abc_val_3_y,
...
FROM data.abc
WHERE abc.date BETWEEN '01-OCT-2009' AND '31-OCT-2009'
GROUP BY abc.date
我不是一個Oracle傢伙,沒有獲得測試甲骨文,所以你可能會發現不少的語法錯誤之類下面。您還需要將硬編碼的日期更改爲變量,聲明一些變量並實際運行創建的查詢。
-- Figure out all valid codes for date range.
-- Might also require ordering depending on your report.
CURSOR c1
IS
SELECT
abc.code
FROM data.abc
WHERE abc.date BETWEEN '01-OCT-2009' AND '31-OCT-2009'
GROUP BY abc.code;
query1 := 'CREATE TABLE abc_report AS SELECT date';
LOOP
FETCH c1 INTO code1
EXIT WHEN c1%NOTFOUND;
query1 := query1 || ', DECODE(code, ''' || code1 || ''', val_1, NULL)) AS abc_val_1_' || code1 ||
', DECODE(code, ''' || code1 || ''', val_2, NULL)) AS abc_val_2_' || code1 ||
', DECODE(code, ''' || code1 || ''', val_3, NULL)) AS abc_val_3_' || code1
END LOOP;
query1 := query1 || ' FROM data.abc WHERE date BETWEEN ''01-OCT-2009'' AND ''31-OCT-2009'''
我建議讓報告工具做任何交叉表/多維數據集/數據透視表,只是擔心查詢返回正確的數據來支持它。 – Brian 2013-03-06 09:37:42