首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求解:学习sqlserver,该如何解决

2012-03-26 
求解:学习sqlserver现在有一张表字段A字段B字段C字段DAABB1011AACC100AADD108AAEE1010现在想查询该数据 如

求解:学习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语句怎么写啊!

[解决办法]

SQL code
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 行受影响)
[解决办法]
探讨
引用:

SQL code
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','1……

[解决办法]
SQL code
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 行受影响) 

热点排行