要包括所有的作業,而無需PIVOT
的IN
條款中明確列出這些,除動態SQL,您可以使用PIVOT XML
和重寫查詢如下:
-- sample of data
with t1(col) as(
select 'CLERK' from dual union all
select 'SALESMAN' from dual union all
select 'MANAGER' from dual
)
select col_xml
from t1
pivot xml(
count(*) for col in(select col from t1)
)
XML結果:
COL_XML
--------------------------------------------------------------------------------
<PivotSet><item><column name = "COL">CLERK</column><column name = "COUNT(*)">1</
column></item><item><column name = "COL">MANAGER</column><column name = "COUNT(*
)">1</column></item><item><column name = "COL">SALESMAN</column><column name = "
COUNT(*)">1</column></item></PivotSet>
但是,然後,得到友好的表示數據喲ü將不得不明確地提取值:
SQL> with t1(col) as(
2 select 'CLERK' from dual union all
3 select 'SALESMAN' from dual union all
4 select 'MANAGER' from dual
5 )
6 select extractvalue(col_xml,'/PivotSet/item[1]/column[2]') col_1
7 , extractvalue(col_xml,'/PivotSet/item[2]/column[2]') col_2
8 , extractvalue(col_xml,'/PivotSet/item[3]/column[2]') col_3
9 from (select col_xml
10 from t1
11 pivot xml(
12 count(*) for col in(select col from t1)
13 )
14 )
15 ;
結果:
COL_1 COL_2 COL_3
-----------------
1 1 1
SQLFiddle Demo
我從[這裏](http://www.eeblog.org/index.php/發現「這個PIVOT運算符有一個很大的侷限性:IN子句的參數不能通過dinamically。」太糟糕了。 – Matkrupp