日期:2014-05-18 浏览次数:20888 次
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([AA] varchar(1),[BB] varchar(4),[CC] int)
insert [test]
select 'X','0101',100 union all
select 'X','0102',30 union all
select 'T','0101',40 union all
select 'G','0102',60 union all
select 'F','0101',10 union all
select 'F','0102',15
select [AA],[BB],
case when [BB]='0101' then [CC] 
when ([BB]='0101' or BB='0102') and (select COUNT(1) from test b where a.AA=b.AA)=1
then  CC else 0  end  [CC]
from test a
/*
AA    BB    CC
X    0101    100
X    0102    0
T    0101    40
G    0102    60
F    0101    10
F    0102    0
*/
------解决方案--------------------