sql查询基于列值运行

时间:2016-06-25 22:48:43

标签: sql oracle

我有一个表格,数据库的值如下:db1

database   sync_status     validation    remark
db1        successful      failed        error
db1        successful      successful    all is well

如果数据库db1的验证成功,我不希望查询返回任何行。但在我的情况下,它返回验证失败的行。

select database,sync_status,validation,remark from bcv_sync where database='db1'  and validation!='successful' and validation='failed' and rownum = 1;

但是如果db1验证失败,查询必须返回验证失败的行。

2 个答案:

答案 0 :(得分:1)

如果一次成功意味着数据库成功,那么您可以使用聚合。我不确定你在其他专栏中想要什么,但这可能会做你需要的:

select database, max(sync_status), max(validation), max(remark)
from bcv_sync
where database = 'db1' 
group by database
having sum(case when validation = 'success' then 1 else 0 end) = 0

答案 1 :(得分:1)

如果我解释你的神秘问题,那么只有在没有成功的行时才显示失败的行:

SELECT database,
       sync_status,
       validation,
       remark
FROM   (
  SELECT d.*,
         COUNT( CASE validation WHEN 'successful' THEN 1 END ) OVER ()
           AS num_success
  FROM   db1 d
)
WHERE  validation = 'failed'
AND    num_success = 0;