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

100分求一句DB2的sql,该怎么解决

2012-03-24 
100分求一句DB2的sqlTable如下:FILE_NAMESTATUSFTP_000006EFTP_000006SFTP_000006SFTP_000007SFTP_000007S

100分求一句DB2的sql
Table如下:
FILE_NAME STATUS
FTP_000006 E
FTP_000006 S
FTP_000006 S
FTP_000007 S
FTP_000007 S
FTP_000007 S
FTP_000008 S
FTP_000008 S
FTP_000008 S

需要select出status全是‘S’的文件名(注意,“status全是S”是重点:比如FTP_000006就不符合,因为它有一个STATUS是E,而不是S)。
此sql搜索出来的结果集应该如下:

FTP_000007
FTP_000008

[解决办法]

select DISTINCT FILE_NAME from ttA a where not exists(select 1 from ttA where a.FILE_NAME=FILE_NAME and STATUS<>'S')
[解决办法]

SQL code
select distinct FILE_NAME from Tablewhere FILE_NAME not in (    select FILE_NAME from Table where STATUS <> 'S')
[解决办法]
SQL code
select distinct FILE_NAME from Table Awhere not exists (    select 1 from Table B where A.FILE_NAME = B.FILE_NAME and B.STATUS <> 'S')
[解决办法]
如果你的STATUS 字段有null的话,需要这样写:
SQL code
select distinct FILE_NAME from Tablewhere FILE_NAME not in (    select FILE_NAME from Table where STATUS <> 'S' or STATUS is null)select distinct FILE_NAME from Table Awhere not exists (    select 1 from Table B where A.FILE_NAME = B.FILE_NAME and B.STATUS <> 'S' or B.STATUS is null)
[解决办法]
OR
SELECT DISTINCT B.FILE_NAME from tta B LEFT JOIN (
SELECT FILE_NAME from tta where STATUS<>'S' ) C ON B.FILE_NAME=C.FILE_NAME WHERE C.FILE_NAME IS NULL

热点排行