检查 sql 中的重复项
check for duplicates in sql
我正在加入两个 tables A 和 B。我的查询看起来像这样。
select id, name, sum(qty)
from table 1
left join table 2 on table1.id = table2.id and table2.column = XXXX
group by 1,2
我想知道此联接是否在 table 1.
中创建任何重复的行
我正在使用 teradata SQL
我了解到您想知道 table2
中是否存在符合您的加入条件的多行。这是一个用于此目的的查询:
select t2.id
from table2 t2
where
t2.colum = 'XXXX'
and exists (select 1 from table1 t1 where t1.id = t2.id)
group by t2.id
having count(*) > 1
该查询返回的任何行都会与您现有连接中的行重复。
一种方法可能是
select id, name, sum(qty)
from table 1 as t1
left join table 2 as t2 on table1.id = table2.id and table2.column = XXXX
where t1.id in (select distinct id from table 1 )
Distinct 检查行区分,您提到唯一需要检查重复项的列是 ID。
我正在加入两个 tables A 和 B。我的查询看起来像这样。
select id, name, sum(qty)
from table 1
left join table 2 on table1.id = table2.id and table2.column = XXXX
group by 1,2
我想知道此联接是否在 table 1.
中创建任何重复的行我正在使用 teradata SQL
我了解到您想知道 table2
中是否存在符合您的加入条件的多行。这是一个用于此目的的查询:
select t2.id
from table2 t2
where
t2.colum = 'XXXX'
and exists (select 1 from table1 t1 where t1.id = t2.id)
group by t2.id
having count(*) > 1
该查询返回的任何行都会与您现有连接中的行重复。
一种方法可能是
select id, name, sum(qty)
from table 1 as t1
left join table 2 as t2 on table1.id = table2.id and table2.column = XXXX
where t1.id in (select distinct id from table 1 )
Distinct 检查行区分,您提到唯一需要检查重复项的列是 ID。