求解:学习sqlserver
现在有一张表
字段A 字段B 字段C 字段D
AA BB 10 11
AA CC 10 0
AA DD 10 8
AA EE 10 10
现在想查询该数据 如果字段D中有字段为0的状态为:缺失;
得到如下:
字段A 状态1
AA 缺失
这个sql语句怎么写啊!
[解决办法]
if object_id('tb') is not null drop table tb GOcreate table tb (A varchar(6) ,B varchar(6),C varchar(6),D varchar(6))goinsert 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 11AA CC 10 缺失AA DD 10 8AA EE 10 10(4 行受影响)
[解决办法]
if object_id('tb') is not null drop table tb GOcreate table tb (A varchar(6) ,B varchar(6),C varchar(6),D varchar(6))goinsert 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 10BB 10BB 10BB 10(5 行受影响)