我有一種情況,我需要將'n'行拆分到列組中。例如,下面是數據集組'n'行到列 - oracle
COMMENT_TEXT
T1
T2
T3
T4
T5
T6
預期輸出:
SUN MON TUE
T1 T2 T3
T4 T5 T6
我的查詢:
SELECT htbp1.comment_text
FROM hxc_time_building_blocks htbp,
hxc_time_building_blocks htbp1
WHERE htbp1.parent_building_block_id = htbp.time_building_block_id
AND htbp1.parent_building_block_ovn = htbp.parent_building_block_ovn
AND htbp.parent_building_block_id = 116166
AND htbp.parent_building_block_ovn = 1
ORDER BY htbp1.time_building_block_id
有沒有什麼方法可以讓我做PIVOT用一個 'n' 行無骨料功能?
編輯:T1/T2/T3作爲樣本數據集,但實際上它可以是任意隨機自由文本或null。
SELECT * FROM (SELECT htbp1.comment_text, TO_CHAR (htbp.start_time, 'DY') par_time,
trunc((rownum-1)/7) buck
FROM hxc_time_building_blocks htbp,
hxc_time_building_blocks htbp1,
hxc_timecard_summary hts
WHERE hts.RESOURCE_ID = :p_resource_id
AND TRUNC(hts.STOP_TIME) = TRUNC(:p_wkend_date)
AND htbp1.parent_building_block_id = htbp.time_building_block_id
AND htbp1.parent_building_block_ovn = htbp.parent_building_block_ovn
AND htbp.parent_building_block_id = hts.timecard_id
AND htbp.parent_building_block_ovn = hts.timecard_ovn
ORDER BY htbp1.time_building_block_id) PIVOT(max(comment_text) FOR par_time
IN ('SUN' AS "SUN",
'MON' AS "MON",
'TUE' AS "TUE",
'WED' AS "WED",
'THU' AS "THU",
'FRI' AS "FRI",
'SAT' AS "SAT"));
當我添加的另一個表「hxc_timecard_summary」,這是父,然後數據是瘋狂,但如果我在第一使用硬編碼的參數,如一個,則行被顯示出來細。
你真棒。非常感謝。第二個PIVOT的工作,但有一個問題,我跑了。你可以看看編輯過的問題嗎? – Phani 2014-11-01 22:28:02
@Phani,trunc((rownum-1)/ 7)buck - 你不需要這個 – Multisync 2014-11-01 22:34:11
@Phani DY - 這是NLS的依賴,所以要小心 – Multisync 2014-11-01 22:35:27