Dynamic pivot in oracle sql

后端 未结 7 986

... pivot (sum(A) for B in (X))

Now B is of datatype varchar2 and X is a string of varchar2 values separated by commas.
Values for X are select distinct values

7条回答
  •  我寻月下人不归
    2020-11-22 00:18

    You can't put a non constant string in the IN clause of the pivot clause.
    You can use Pivot XML for that.

    From documentation:

    subquery A subquery is used only in conjunction with the XML keyword. When you specify a subquery, all values found by the subquery are used for pivoting

    It should look like this:

    select xmlserialize(content t.B_XML) from t_aa
    pivot xml(
    sum(A) for B in(any)
    ) t;
    

    You can also have a subquery instead of the ANY keyword:

    select xmlserialize(content t.B_XML) from t_aa
    pivot xml(
    sum(A) for B in (select cl from t_bb)
    ) t;
    

    Here is a sqlfiddle demo

提交回复
热议问题