0

わかりましたので、以下のように 1 日あたりの型の数を見つけるために、このクエリがあります。(MarkD の厚意により、DB 内のアイテム/行を別の列でグループ化された列として数えます)

 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 に答える