`
pipe
  • 浏览: 205439 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

单表多字段统计

    博客分类:
  • db
SQL 
阅读更多
在表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'这个是传递进来的参数。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics