根据一个值删除重复项
Removing duplicates based on one value
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 查询,它给出了上述结果,我希望该结果能删除任何有滞纳金的客户
输出需要是:
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
我不确定你的表格到底是什么样子,但你可以这样做:
WHERE customer_id NOT IN (
SELECT customer_id
FROM table_with_customer_and_pay_type
WHERE pay_type = 'late'
GROUP BY customer_id )
我会这样做作为反加入:
select *
from table a
where not exists (
select null
from table b
where
a.customer_id = b.customer_id and
b.pay_type = 'late'
)
这优于独特的或 "not in" 方法,因为它会在找到匹配项后停止查找。这应该对大型和小型数据集都有效。
任何使用不同的解决方案都必须评估整个数据集,然后删除重复项。
常见Table 表达变体:
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
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 查询,它给出了上述结果,我希望该结果能删除任何有滞纳金的客户
输出需要是:
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
我不确定你的表格到底是什么样子,但你可以这样做:
WHERE customer_id NOT IN (
SELECT customer_id
FROM table_with_customer_and_pay_type
WHERE pay_type = 'late'
GROUP BY customer_id )
我会这样做作为反加入:
select *
from table a
where not exists (
select null
from table b
where
a.customer_id = b.customer_id and
b.pay_type = 'late'
)
这优于独特的或 "not in" 方法,因为它会在找到匹配项后停止查找。这应该对大型和小型数据集都有效。
任何使用不同的解决方案都必须评估整个数据集,然后删除重复项。
常见Table 表达变体:
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