当前位置: 代码迷 >> DB2 >> 100分求一句DB2的sql,该如何处理
  详细解决方案

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

热度:7176   发布时间:2013-02-26 00:00:00.0
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
  相关解决方案