SQL CASE语句数据组织成一个查询

时间:2014-06-24 19:35:09

标签: sql case

我有以下查询,我按日期分解事故。

select t.range as [Date Range], count(*) as Accidents
from (
    select case
        when (MONTH(accidentdatetime) >= 6 AND YEAR(accidentdatetime) = 2013) and (MONTH(accidentdatetime) <= 12 AND YEAR(accidentdatetime) = 2013) then 'Other'
        when MONTH(accidentdatetime) = 1 AND YEAR(accidentdatetime) = 2014 then 'January'
        when MONTH(accidentdatetime) = 2 AND YEAR(accidentdatetime) = 2014 then 'Feburary'
        when MONTH(accidentdatetime) = 3 AND YEAR(accidentdatetime) = 2014 then 'March'
        when MONTH(accidentdatetime) = 4 AND YEAR(accidentdatetime) = 2014 then 'April'
        when MONTH(accidentdatetime) = 5 AND YEAR(accidentdatetime) = 2014 then 'May'
        end as range
    from Accidents
    where accidentdatetime between '06/30/2013' and '05/31/2014'
    ) t
group by t.range

enter image description here

我试图理解如何进一步将其分解为可以根据数据库中的OSHA字段进一步指定事故类型的地方。类似的东西:

select t.range as [Date Range],count(*) as [Accidents], (SELECT count(*) FROM Accidents WHERE OSHAClassificationID IN (2,4)) as [Lost Time]
from (
    select case
        when (MONTH(accidentdatetime) >= 6 AND YEAR(accidentdatetime) = 2013) and (MONTH(accidentdatetime) <= 12 AND YEAR(accidentdatetime) = 2013) then 'Other'
        when MONTH(accidentdatetime) = 1 AND YEAR(accidentdatetime) = 2014 then 'January'
        when MONTH(accidentdatetime) = 2 AND YEAR(accidentdatetime) = 2014 then 'Feburary'
        when MONTH(accidentdatetime) = 3 AND YEAR(accidentdatetime) = 2014 then 'March'
        when MONTH(accidentdatetime) = 4 AND YEAR(accidentdatetime) = 2014 then 'April'
        when MONTH(accidentdatetime) = 5 AND YEAR(accidentdatetime) = 2014 then 'May'
        end as range
    from Accidents
    where accidentdatetime between '06/30/2013' and '05/31/2014'
    ) t
group by t.range

enter image description here

但是我创建另一个只指定一种事故类型的列的尝试没有按日期过滤,只是根据我的查询显示数据库中所有丢失时间事故的计数。

有没有办法在一个查询中按日期范围和事故类型进行过滤?

1 个答案:

答案 0 :(得分:1)

您需要返回一个标志,以指示记录是否在您的osha范围内但在月份过滤器内。然后用1来总结记录,以获得代码子集中的计数。

select t.range as [Date Range],count(*) as [Accidents], 
    [Lost Time]=sum(OSHAFlag)
from (
    select 
       OSHAFlag=case when Accidents.OSHAClassificationID IN(2,4) THEN 1 ELSE 0 END,
       case
        when (MONTH(accidentdatetime) >= 6 AND YEAR(accidentdatetime) = 2013) and (MONTH(accidentdatetime) <= 12 AND YEAR(accidentdatetime) = 2013) then 'Other'
        when MONTH(accidentdatetime) = 1 AND YEAR(accidentdatetime) = 2014 then 'January'
        when MONTH(accidentdatetime) = 2 AND YEAR(accidentdatetime) = 2014 then 'Feburary'
        when MONTH(accidentdatetime) = 3 AND YEAR(accidentdatetime) = 2014 then 'March'
        when MONTH(accidentdatetime) = 4 AND YEAR(accidentdatetime) = 2014 then 'April'
        when MONTH(accidentdatetime) = 5 AND YEAR(accidentdatetime) = 2014 then 'May'
        end as range
    from Accidents
    where accidentdatetime between '06/30/2013' and '05/31/2014'
    ) t
group by t.range