简体   繁体   English

Select 仅非重复行 (SQL)

[英]Select Only Non Duplicated Rows (SQL)

When making a left join with another SQL table some of my left tables rows are being duplicated and joined with multiple right table rows, since in my usage this means these are ambiguous rows I need to be able to filter rows which have the same value as each other.当与另一个 SQL 表进行左连接时,我的一些左表行被复制并与多个右表行连接,因为在我的使用中这意味着这些是模棱两可的行我需要能够过滤具有相同值的行彼此。

ie IE

Select Table_1.Id, Table_1.Value, Table_2.Id, Table_2.Value
From Table_1 Left Join Table_2 on Table_1.Value = Table_2.Value

Resulting in:导致:

Table_1.Id,     Table_1.Value,  Table_2.Id,     Table_2.Value
1               T               2               T
2               G               3               G
2               G               5               G
3               K               1               K
4               M               4               M

However I want to create a query that results in:但是我想创建一个查询结果:

Table_1.Id,     Table_1.Value,  Table_2.Id,     Table_2.Value
1               T               2               T
3               K               1               K
4               M               4               M

Where Since Row 2 had joined with multiple rows in Table_2 it is rejected from the query.由于第 2 行已与 Table_2 中的多行连接,因此它被查询拒绝。

I've Had a look at Finding duplicate values in a SQL table and have been able to use it to find duplicates within Table_1 but not rows that have been connected to multiple Table_2 rows.我已经查看了在 SQL 表中查找重复值,并且已经能够使用它来查找 Table_1 中的重复项,但不能查找已连接到多个 Table_2 行的行。 Any help would be greatly appreciated.任何帮助将不胜感激。

You can use count() as a window function:您可以使用count()作为 window function:

Select t1.Id, t1.Value, t2.Id, t2.Value
From Table_1 Join
     (select t2.*,
             count(*) over (partition by t2.value) as cnt
      from Table_2 t2
     ) t2
     on t2.Value = t1.Value
where t2.cnt = 1;

Based on your sample data, left join does not seem appropriate.根据您的示例数据, left join似乎不合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM