ITeye博客

单表多字段统计

pipe · 2009-07-27 11:16 · db · Markdown

在表CCCommonCompany中 有province字段,licenseStartDate 开始日期字段,licenseEndDate 结束日期字段, 我们的要求是按照licenseStartDate和licenseEndDate所在的年份来统计.见sql:


select  province,   
sum(case    when   year(licenseStartDate)='2012'  then   1 else 0    end)    as  startDateCount     ,     
sum(case    when   year(licenseEndDate)='2012'  then   1 else 0   end)    as  endDateCount
from CCCommonCompany     
GROUP BY province 

说明: sum(case when year(licenseStartDate)='2012' then 1 else 0 end) as startDateCount
是指 当licenseStartDate的年份='2012'的时候 就+1 否则+0

当然'2012'这个是传递进来的参数。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。