---
title: "单表多字段统计"
author: "pipe"
date: "2009-07-27T11:16:23Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-07-27T11:16:23Z"
id: 433660
url: "https://pipe.iteye.com/blog/433660"
---

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

```java

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'这个是传递进来的参数。