1

データ

region class_month attendance
NY  12/1/2011   70444    
NY  1/1/2012    70125
NY  2/1/2012    69582
NY  3/1/2012    71529
NY  4/1/2012    72468
NY  5/1/2012    67068
LA  3/1/2012    1638
LA  4/1/2012    3079
LA  5/1/2012    4205

結果を次のようにしたいと思います。

  region class_month attendance
    NY  1/1/2012    70125
    NY  2/1/2012    69582
    NY  3/1/2012    71529
    NY  4/1/2012    72468
    NY  5/1/2012    67068
    LA  1/1/2012    0
    LA  2/1/2012    0
    LA  3/1/2012    1638
    LA  4/1/2012    3079
    LA  5/1/2012    4205

クエリ

SELECT a.region
    ,a.class_month
    ,CASE 
        WHEN a.attendance IS NULL
            THEN 0
        ELSE a.attendance
        END AS attendance -- this clearly isn't right
FROM dbo.mbo_monthly_attendance a
where class_month between '2012-01-01' and '2012-05-01'

LAが提供された日付範囲に何もなかった月に、出席が0の行を返すにはどうすればよいですか?

正しい方向へのプッシュをありがとう。

4

2 に答える 2

1

編集2:これはうまくいくと思います:

select mr.region, mr.class_month, coalesce(a.attendance, 0) as attendance
from (select * from 
 (select distinct region as region from mbo_monthly_attendance) r,
 (select distinct class_month as class_month from mbo_monthly_attendance) c) mr 
LEFT OUTER JOIN mbo_monthly_attendance a 
  ON mr.class_month = a.class_month AND mr.region = a.region ;

ビューごとに分割して、理解しやすくすることができます。

create view regions as select distinct region from mbo_monthly_attendance;
create view class_months as select distinct class_month from mbo_monthly_attendance;
create view region_months as select * from regions, class_months;

select mr.region, mr.class_month, coalesce(a.attendance, 0) as attendance
from region_months mr LEFT OUTER JOIN mbo_monthly_attendance a 
  ON mr.class_month = a.class_month AND mr.region = a.region ;

「クラス月」のテーブルを作成し、月次出席テーブルで外部結合を使用します。

create table months (class_month date);
insert into months values ('1/1/2012');
insert into months values ('2/1/2012');
insert into months values ('3/1/2012');
insert into months values ('4/1/2012');
insert into months values ('5/1/2012');
insert into months values ('6/1/2012');

SELECT a.region
    ,a.class_month
    ,COALESCE (a.attendance, 0) AS attendance
FROM dbo.mbo_monthly_attendance a 
    LEFT OUTER JOIN months m ON a.class_month = m.class_month 
WHERE m.class_month between @StartDate and @EndDate;

別のテーブルを作成するのではなく、出席テーブルを使用してすべての月を表示することができます。

create view months as select distinct class_month from mbo_monthly_attendance
于 2013-03-11T18:48:04.897 に答える
0

これを試して

DECLARE @startdate datetime, @enddate datetime
set @startdate = '2012-01-01'
set @enddate = '2012-05-01'

;with cte as
(
    select @startdate DateValue
    union all
    select DateValue + 1
    from    cte   
    where   DateValue + 1 < @enddate
), cte2 as
(
    select distinct region, DateValue from dbo.mbo_monthly_attendance a 
    outer apply (select * from cte) x
)


select 
d.region,d.DateValue class_month,COALESCE (a.attendance, 0) AS attendance
from cte2 d
LEFT outer join dbo.mbo_monthly_attendance a
on CONVERT(varchar(15),d.DateValue,101) = CONVERT(varchar(15),a.class_month,101)    
order by d.region,d.DateValue
OPTION (MAXRECURSION 0) 
于 2013-03-11T19:05:49.067 に答える