查找重复记录

时间:2019-05-07 01:33:46

标签: sql oracle

表如下:

Request#  type  status
123         R     partially complete 
123         S     complete
345         S     partially complete
345         S     complete

R表示短请求,S表示长请求。状态为“完成”时,每种请求类型都将变为S。我需要找到所有已完成的“ R”类请求

select * 
from table 
where type ='R' and status='partially complete' OR 
      type ='s' and status='complete'

预期结果

Request#  type  
123         R

3 个答案:

答案 0 :(得分:1)

我想你想要

select Request#, min(type) as type
  from "table" 
 group by Request#
 having min(status)='complete'

答案 1 :(得分:1)

这是Barbaros解决方案的概括:

select Request#
from "table" 
group by Request#
having sum(case when status = 'complete' then 1 else 0 end) > 0 and
       sum(case when type = 'R' then 1 else 0 end) > 0;

答案 2 :(得分:0)

一种简单的方法使用聚合:

SELECT
    request
FROM yourTable
GROUP BY
    request
HAVING
    COUNT(CASE WHEN type = 'R' AND status = 'partially complete' THEN 1 END) > 0 AND
    COUNT(CASE WHEN type = 'S' AND status = 'complete' THEN 1 END) > 0;

我们也许还可以使用自我联接来表达这一点:

SELECT DISTINCT t1.request
FROM yourTable t1
INNER JOIN yourTable t2
    ON t1.request = t2.request AND
       t2.type = 'S' AND t2.status = 'complete'
WHERE
    t1.type = 'R' AND
    t1.status = 'partially complete';