读书人

100分求一句DB2的sql,该如何处理

发布时间: 2012-12-29 10:28:09 作者: rapoo

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')
[解决办法]

select distinct FILE_NAME from Table
where FILE_NAME not in (
select FILE_NAME from Table where STATUS <> 'S'
)

[解决办法]
select distinct FILE_NAME from Table A
where not exists (
select 1 from Table B where A.FILE_NAME = B.FILE_NAME and B.STATUS <> 'S'
)

[解决办法]
如果你的STATUS 字段有null的话,需要这样写:
select distinct FILE_NAME from Table
where FILE_NAME not in (
select FILE_NAME from Table where STATUS <> 'S' or STATUS is null
)

select distinct FILE_NAME from Table A
where 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

读书人网 >IBM DB2

热点推荐