枢轴表达式中的条件

时间:2017-09-07 12:06:32

标签: sql oracle

有没有什么方法可以使用大于数据透视表达?例如:

SELECT
    MONTH
  , col1
  , col2
FROM
(
   SELECT month, c1, c2, date1 FROM table1
)
PIVOT
(
   SUM(c1)
   FOR(c2, date1) IN
   (
        ('x', < SYSDATE)  AS col1
      , ('x', >= SYSDATE) AS col2
   )
);

我需要有来自sysdate的列。

1 个答案:

答案 0 :(得分:3)

只需使用条件聚合。有点不清楚你想做什么,但这应该很接近:

select month,
       sum(case when date1 < sysdate then c1 else 0 end) as col1,
       sum(case when date1 >= sysdate then c1 else 0 end) as col2
from table1 t1
group by month