如何过滤 table 中具有不同条件的记录

How to filter record in a table with different condition

如何在 table 中找到仅 'Return' 作为 Reason 的记录?

我有table,其中有很多条记录

根据table中的数据,它returns只记录保单号5555,因为只有那个保单号只有'Return';所有其他保单号码都有 'Return''Success',我不需要。

请帮助我。

SELECT *
FROM YOURTABLE
WHERE REASON = 'RETURN'
AND POLICYNUMBER NOT IN (
    SELECT POLICYNUMBER
    FROM YOURTABLE
    WHERE REASON = 'SUCCESS'
)

我会用 not exists:

select t.*
from mytable t
where not exists (
    select 1 
    from mytable t1 
    where t1.policynumber = t.policynumber and t1.reason <> 'Return'
)

此查询将利用 (policynumber, reason) 上的索引。