日期:2014-05-18 浏览次数:20520 次
create table tb(日期 datetime , 产地 varchar(10)) insert into tb values('2012-01-01 0:05:35', 'CN') insert into tb values('2012-01-01 0:07:35', 'CN') insert into tb values('2012-01-01 1:09:35', 'US') insert into tb values('2012-01-01 4:05:35', 'CN') insert into tb values('2012-01-02 0:06:35', 'US') insert into tb values('2012-01-02 0:07:35', 'US') insert into tb values('2012-01-03 5:07:43', 'CN') insert into tb values('2012-01-03 0:06:35', 'US') insert into tb values('2012-01-04 6:02:52', 'US') go select convert(varchar(10),日期,120) 日期, count(1) [All], sum(case when 产地 = 'US' then 1 else 0 end) [US], sum(case when 产地 = 'CN' then 1 else 0 end) [CN] from tb group by convert(varchar(10),日期,120) /* 日期 All US CN ---------- ----------- ----------- ----------- 2012-01-01 4 1 3 2012-01-02 2 2 0 2012-01-03 2 1 1 2012-01-04 1 1 0 (所影响的行数为 4 行) */ select convert(varchar(10),日期,120) 日期, count(1) [All], sum(case when 产地 = 'US' then 1 else 0 end) [US], sum(case when 产地 = 'CN' then 1 else 0 end) [CN] from tb group by convert(varchar(10),日期,120) union all select '合计' , count(1) [All], sum(case when 产地 = 'US' then 1 else 0 end) [US], sum(case when 产地 = 'CN' then 1 else 0 end) [CN] from tb /* 日期 All US CN ---------- ----------- ----------- ----------- 2012-01-01 4 1 3 2012-01-02 2 2 0 2012-01-03 2 1 1 2012-01-04 1 1 0 合计 9 5 4 (所影响的行数为 5 行) */ drop table tb