2013-03-11 111 views
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' 

我如何讓洛杉磯回來的行有0個月沒有提供的日期範圍?

感謝您的正確方向。

+0

'COALESCE'會比簡單的' CASE'條款。 – 2013-03-11 19:10:29

回答

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 
+0

在你的第一個查詢中,如果紐約和洛杉磯都只允許說3個日期值不是全部,你將如何返回缺少2個日期的數據? – 2013-03-11 19:15:16

+0

謝謝!我有這個爲我工作。 – duffn 2013-03-11 19:20:09

+0

@rs - 是的,它確實需要至少一個區域在特定月份中有一行。唯一的解決方法是以某種方式創建一個日期表(如我的原始編輯前建議) – 2013-03-11 19:22:00

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) 
相關問題