PIVOT或PIVOT XML没有在Oracle上显式列出列

时间:2014-10-14 12:06:52

标签: sql xml oracle pivot pivot-xml

我正在研究Oracle 11g数据库上的PL / SQL Developer v10。 这个想法不是写PIVOT语句中列出的145列,而是写Pivot XML并从那里获取数据I am working on a basis of this answer,,因为我之前从未在Oracle上做过PIVOT XML,所以我的期望可能是错误的

select * from (
                    select
                      tqs.transactiondetailid as transactiondetailid,
                      q.productid as productid,
                      tqs.answer as QAnswer,
                      regexp_substr(q.questiondescription,'(WIF|DT|WT)[([:digit:]|.)]*') as QDesc,
                      tqs.transactionversion as transactionversion
                    from TRANSACTIONDETAILQS tqs
                    inner join question q on q.questionid = tqs.questionid and (
                          q.questiondescription like 'DT%' 
                          or  q.questiondescription like 'WT%' 
                          or q.questiondescription like 'WIF%')
    ) pivot xml(
    min(QAnswer) for QDesc in (
    select regexp_substr(q.questiondescription,'(WIF|DT|WT)[([:digit:]|.)]*') as r from question q
    where regexp_substr(q.questiondescription,'(WIF|DT|WT)[([:digit:]|.)]*') IS NOT NULL)
    )
XML列中的

不会返回任何数据,它们都是空的,但是下面的语句我列出的列明确地工作正常

select * from (
                    select
                      tqs.transactiondetailid as transactiondetailid,
                      q.productid as productid,
                      tqs.answer as QAnswer,
                      regexp_substr(q.questiondescription,'(WIF|DT|WT)[([:digit:]|.)]*') as QDesc,
                      tqs.transactionversion as transactionversion
                    from TRANSACTIONDETAILQS tqs
                    inner join question q on q.questionid = tqs.questionid and (
                          q.questiondescription like 'DT%' 
                          or  q.questiondescription like 'WT%' 
                          or q.questiondescription like 'WIF%')
    ) pivot (
    min(QAnswer) for QDesc in (
    'DT01.','DT02.','WT01.')
    )

如何在不明确列出列的情况下进行透视?

1 个答案:

答案 0 :(得分:2)

我把它归结为

的问题
min(QAnswer) for QDesc In (
    select 'DT01.','DT02.','WT01.' from dual )
) 

不工作,但

min(QAnswer) for QDesc In (
    'DT01.','DT02.','WT01.')
)

工作正常。

执行此操作的一种方法是create dynamic PL/SQL,但我没有足够的勇气去做这件事。