日期:2014-05-17 浏览次数:20430 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (id nvarchar(2),state int,times int)
insert into [TB]
select 'a',1,0 union all
select 'a',0,13 union all
select 'a',1,0 union all
select 'a',0,46 union all
select 'b',0,566 union all
select 'b',1,0 union all
select 'b',0,15 union all
select 'c',1,0 union all
select 'c',0,480 union all
select 'd',0,960 union all
select 'd',1,0 union all
select 'd',0,90
select * from [TB]
SELECT id ,
SUM(CASE WHEN STATE = 0
&nbs