我想获得我想要的输出的最佳方法是执行以下操作:
define startDate = to_date('2013-10-01', 'yyyy-mm-dd')
select type,
sum(case when MyDate = &&startDate then 1 else 0 end) as "1"
, sum(case when MyDate = &&startDate +1 then 1 else 0 end) as "2"
, sum(case when MyDate = &&startDate +2 then 1 else 0 end) as "3"
, sum(case when MyDate = &&startDate +3 then 1 else 0 end) as "4"
...etc for as many days of current month I am running
, sum(case when MyDate = &&startDate +29 then 1 else 0 end) as "30"
, sum(case when MyDate = &&startDate +30 then 1 else 0 end) as "31"--This would be commented out for Nov
from MyTabe
group by type
order by type
;
这样,如果我想在 11 月、12 月、1 月等时间运行它,我只需更改顶部的变量并运行查询。这就是我一直在寻找的;但是,我仍然想知道是否可以动态生成列,但是我越看它,我就越认为需要数据透视表。