删除 'All' DAY 值行,该行具有 1 个 ID 的多个数据值,其中之一是 'All'

Remove the 'All' DAT value rows which has multiple DAT values for 1 ID and one of them is 'All'

如何删除 'All' 个 DAT 值行,其中 1 个 ID 有多个 DAT 值,其中之一是 'All'。但是,如果 val 只有 'All' 作为 DAT 值,我们应该包括这些行:

示例输入:

示例输出:

如有任何帮助,我们将不胜感激。提前致谢!

DELETE FROM
  your_table
WHERE
  EXISTS (
    SELECT *
      FROM your_table  lookup
     WHERE lookup.id   = your_table.id
       AND lookup.val  = your_table.val
       AND lookup.dat <> 'All'
  )
  AND dat = 'All'

如果您只想要一个 select 查询,那么 window 函数会有所帮助:

select t.*
from (select t.*,
             sum( case when data <> 'All' then 1 else 0 end) over (partition by id, val) as cnt_notall
      from t
     ) t
where not (data = 'All' and cnt_notall > 0);