oracle:在动态日期上转动

时间:2014-10-14 13:03:56

标签: sql oracle pivot

我有这个问题:

select pvt1.*
from 
(
   select
      TO_CHAR(DateAppointment, 'yyyy-mm-dd') as currentDay,
      count(*) myCounter
      [...]

   from (
      select 
         [...]
      from myTable

   ) a
   group by [...]
   order by DateAppointment
) source1
PIVOT
(
    max(myCounter)
    --FOR currentDay IN ('2012-08-20', '2012-08-21', '2012-08-27', '2012-09-03')
    FOR currentDay IN (
      SELECT  LISTAGG(datevalue, ', ')
      WITHIN GROUP (ORDER BY datevalue) 
      FROM DATESLIST 
        ) 
)  pvt1;

此子查询只是从另一个表(DATESLIST)获取我的日期列表,但是当我运行第一个查询时,Oracle会返回错误。

SELECT  LISTAGG(datevalue, ', ')
          WITHIN GROUP (ORDER BY datevalue) 
          FROM DATESLIST 

但是当我使用以下代码时,我得到了正确的结果:

FOR currentDay IN ('2012-08-20', '2012-08-21', '2012-08-27', '2012-09-03')

有什么想法吗? 提前谢谢。

0 个答案:

没有答案