0

好的,所以我有这个查询来查找每天的类型数量,如下所示。(此处由 MarkD 提供Counting Items/Rows in DB as Columns Grouped by another Column

 select type,
           sum(case when MyDate = '' then 1 else 0 end) as "10/1",
           sum(case when MyDate = '' then 1 else 0 end) as "10/2
           ...etc
 from MyTabe
 group by type

但是,我想动态创建日期列并为每列生成计数,否则我最终会为每个月的每一天手动添加一个新列。

4

1 回答 1

0

我想获得我想要的输出的最佳方法是执行以下操作:

 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 月等时间运行它,我只需更改顶部的变量并运行查询。这就是我一直在寻找的;但是,我仍然想知道是否可以动态生成列,但是我越看它,我就越认为需要数据透视表。

于 2014-01-23T22:22:53.710 回答