[SQL ] 新手求救!關於PIVOT的語法
資料庫名稱:ORACLE
資料庫版本:12c
內容/問題描述:小弟我用PIVOT寫一個轉置的查詢Table
原先的語法如下=>
declare
sqlqry clob;
cols clob;
begin
select listagg(''''|| PS_C3 ||'''', ',') within group (order by PS_C3)
into cols
from (select distinct PS_C3 from pick2_1);
sqlqry :=
'
create or replace view pick2_2 as
select * from
(
select num_po, datdels, nam_cust,xfer, PS_C3, qty
from pick2_1
)
pivot
(
sum(qty) for PS_C3 in (' || cols || ')
)';
execute immediate sqlqry;
end;
/
後來RUN發現ps_c3這個欄位組成的字串過長 (listagg好像有4000字限制)
於是改用xmlagg的方式組字串
語法如下=>
declare
sqlqry clob;
cols clob;
begin
select
xmlagg(xmlparse(content ''''|| PS_C3 ||''''||',' wellformed) order by
ps_c3).getclobval()
into cols
from (select distinct ps_c3 from pick2_1);
sqlqry :=
'create or replace view pick2_2 as
select * from
(
select num_po, datdels, nam_cust,xfer, PS_C3, qty
from pick2_1
)
pivot
(
sum(qty) for PS_C3 in ( '|| cols ||' )
)';
execute immediate sqlqry;
end;
/
資料庫會出現報錯訊息"ORA-00936:遺漏表示式"在line24
(sum(qty) from PS_C3 in ('|| cols ||') <=這一句
小弟新手卡一段時間了,請各位高手指點迷津, 感謝~
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.130.11.121 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Database/M.1560915237.A.5EE.html
→
06/19 16:39,
5年前
, 1F
06/19 16:39, 1F
→
06/19 17:13,
5年前
, 2F
06/19 17:13, 2F
→
06/19 17:13,
5年前
, 3F
06/19 17:13, 3F
→
06/19 17:50,
5年前
, 4F
06/19 17:50, 4F
→
06/19 17:51,
5年前
, 5F
06/19 17:51, 5F
→
06/19 17:59,
5年前
, 6F
06/19 17:59, 6F
→
06/19 18:05,
5年前
, 7F
06/19 18:05, 7F
→
06/19 18:07,
5年前
, 8F
06/19 18:07, 8F
→
06/19 19:57,
5年前
, 9F
06/19 19:57, 9F
Database 近期熱門文章
PTT數位生活區 即時熱門文章