SQL显示几个月的结果 - 如何只显示当前年份?

时间:2012-05-16 08:44:45

标签: sql db2

我的SQL表达式工作正常显示结果数月,今年也可以。但是一年后,我知道它将包括今年的结果。如何仅显示当年的结果?

select case month(timestamp_iso(STATUSDATE))
        when 1 then 'January'
        when 2 then 'February'
        when 3 then 'March'
        when 4 then 'April'
        when 5 then 'May'
        when 6 then 'Jun'
        when 7 then 'July'
        when 8 then 'August'
        when 9 then 'September' 
        when 10 then 'October'
        when 11 then 'November'
        when 12 then 'December'
    end as Month, 

    count (case when service='ADSL' then 1 end) as  ADSL,
    count (*) as ALL

from INCIDENT
group by month(timestamp_iso(STATUSDATE))

由于

2 个答案:

答案 0 :(得分:3)

在末尾添加where子句:

where year(STATUSDATE) = year(current_timestamp)

答案 1 :(得分:2)

只需添加where条款!

您可以使用monthname获取您在案例中所做的事情:

select MONTHNAME(timestamp_iso(STATUSDATE)) as Month, 
    count (case when service='ADSL' then 1 end) as  ADSL,
    count (*) as ALL

from INCIDENT
where year(STATUSDATE) = year(current_timestamp)
group by month(timestamp_iso(STATUSDATE))

有关详情,请查看THISTHIS