【问题标题】:Dynamic Pivoting in OracleOracle 中的动态透视
【发布时间】:2018-12-25 13:05:43
【问题描述】:

您好,我想在结构为的表格上应用动态旋转

ID  Type    Amount
--- ------  ------
1   AB      50
2   PQR     100
3   AB      60
4   PQR     120

我想要以下格式的结果:

在我的表中,每个月键入列的值都会发生变化。因此,我希望动态透视表值以获得所需的结果。我尝试按照语法进行旋转,但是每当我尝试将子查询放在旋转的 IN 运算符中时,它都会出错。我正在使用 Oracle 10 g。 谁能在这个问题上帮助我。谢谢。

Select * from(
              Select ID , Type, Value 
                from mytable)x
               pivot(sum(Value) for Type IN (Select distinct Type from myTable))

【问题讨论】:

    标签: sql oracle pivot


    【解决方案1】:

    如果您想获得动态结果,您可能更喜欢使用pivotingxml 选项

    with t(ID, Type, Amount) as
    (
     select 1,'AB',50 from dual union all
     select 2,'PQR',100 from dual union all
     select 3,'AB',60 from dual union all
     select 4,'PQR',120 from dual
    )
    select * 
      from(
           select ID , Type, Amount 
             from t )
        pivot xml( 
                   sum(Amount) as sum_amount for (type) 
                     in (Select distinct Type from t)
                  );
    

    Rextester Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      相关资源
      最近更新 更多