日期:2014-05-18 浏览次数:20527 次
if object_id('tb') is not null drop table tb GO create table tb (A varchar(6) ,B varchar(6),C varchar(6),D varchar(6)) go insert into tb select 'AA','BB','10','11' insert into tb select 'AA','CC','10','0' insert into tb select 'AA','DD','10','8' insert into tb select 'AA','EE','10','10' SELECT A,B ,C,CASE D WHEN '0' THEN '缺失' ELSE D END AS D FROM tb (1 行受影响) (1 行受影响) (1 行受影响) (1 行受影响) A B C D ------ ------ ------ ------ AA BB 10 11 AA CC 10 缺失 AA DD 10 8 AA EE 10 10 (4 行受影响)
------解决方案--------------------
if object_id('tb') is not null drop table tb GO create table tb (A varchar(6) ,B varchar(6),C varchar(6),D varchar(6)) go insert into tb select 'AA','BB','10','11' insert into tb select 'AA','CC','10','0' insert into tb select 'AA','DD','10','8' insert into tb select 'AA','EE','10','10' insert into tb select 'AA','EE','10','10' insert into tb select 'BB','EE','10','10' insert into tb select 'BB','EE','10','10' insert into tb select 'BB','EE','10','10' insert into tb select 'BB','EE','10','10' select A , CASE D WHEN '0' THEN '缺失' ELSE D END AS D FROM ( SELECT A, D FROM tb WHERE D='0' UNION ALL SELECT A,D FROM tb WHERE A NOT IN(SELECT A FROM tb WHERE D='0' ) )s A D ------ ------ AA 缺失 BB 10 BB 10 BB 10 BB 10 (5 行受影响)