首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于一个值删除重复项

基于一个值删除重复项
EN

Stack Overflow用户
提问于 2018-10-16 20:57:31
回答 3查看 43关注 0票数 0
代码语言:javascript
复制
    customer id name    Pay_type
    1111    aaaa    regular
    1111    aaaa    late
    1111    aaaa    regular
    1111    aaaa    regular
    2222    bbbb    regular
    2222    bbbb    regular
    2222    bbbb    regular
    3333    cccc    regular
    3333    cccc    late
    4444    dddd    regular
    4444    dddd    regular

我有一个SQL查询,它给我提供了上面的结果,我希望结果能够删除任何有滞纳金的客户。

产出必须是:

代码语言:javascript
复制
customer id name    Pay_type
2222    bbbb    regular
2222    bbbb    regular
2222    bbbb    regular
4444    dddd    regular
4444    dddd    regular

select 
distinct a.customer_id, 
a.name, 
pay_type 
from table a 
left join table b on a.customer_id= b.id 
left join table c on c.id = b.pay_id 
where b.status = 'Done
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-10-16 22:06:06

我这样做是为了反对加入:

代码语言:javascript
复制
select *
from table a
where not exists (
  select null
  from table b
  where
    a.customer_id = b.customer_id and
    b.pay_type = 'late'
)

这与一种独特的或“不存在”的方法相比有其优点,因为它将停止关注它找到匹配。这对于大数据集和小数据集都应该有效地工作。

任何使用distinct的解决方案都必须评估整个数据集,然后删除dupes。

票数 0
EN

Stack Overflow用户

发布于 2018-10-16 21:39:47

我不知道你的桌子到底是什么样子,但你可以这样做:

代码语言:javascript
复制
WHERE customer_id NOT IN (
    SELECT customer_id
    FROM table_with_customer_and_pay_type
    WHERE pay_type = 'late'
    GROUP BY customer_id )
票数 0
EN

Stack Overflow用户

发布于 2018-10-16 22:09:19

常见表式变体:

代码语言:javascript
复制
WITH orig_result_set AS (
    select 
    distinct a.customer_id, 
    a.name, 
    pay_type 
    from table a 
    left join table b on a.customer_id= b.id 
    left join table c on c.id = b.pay_id 
    where b.status = 'Done'
),

exclude_late_payments AS (
    SELECT DISTINCT customer_id
    FROM orig_result_set
    WHERE pay_type = 'late'
),

on_time_payments AS (
    SELECT customer_id,
           name,
           pay_type
    FROM orig_result_set
    WHERE customer_id NOT IN exclude_late_payments
)

SELECT *
FROM on_time_payments
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52843954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档