用于查找数据匹配或实体解析误报的 SQL 查询

标签 sql sql-server entity resolution matching

假设我为同一条记录分配了两个不同的 ID。例如

RecordID | ID1 | ID2
--------------------
1        | X   | A
2        | X   | B
3        | Y   | C
4        | Y   | C
5        | Y   | D
6        | Z   | E
7        | Z   | E

Now, I want to get the records where ID1 is assigned to the same value where as ID2 is assigned to a different value.

For example, I want to get:

1, X, A
2, X, B

这里 ID1 分配了 X,而 ID2 分配了 A 和 B,两个不同的值。

是否可以在 SQL 或 SQL Server 中编写返回此类记录的查询?

最佳答案

您需要使用子查询,对于每一行,您都可以浏览表并查看是否有任何其他行符合与其相关的特定条件。

伪sql:

select 
      t1.id, 
      t1.variable, 
      t1.value 
   from 
      table t1 
   where 
      exists ( select 1 
                  from t2 
                  where t2.id != t1.id 
                    and t2.variable == t1.variable 
                    and t2.value != t1.value)

关于用于查找数据匹配或实体解析误报的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14739134/

相关文章:

sql - 如何将 PostgreSQL 转储文件恢复到 Postgres 数据库中?

SQL Azure Rest api BeginExport...如何检查导出是否完成

c# - EF6 代码首先添加迁移创建异常迁移但应用程序运行

c# - Entity Framework - 更新表中的一行

c# - Entity Framework Linq、Left Join 和 Group with SUM 和 Count

mysql - 习题帮助——选择平均分最高的学生登记号

mysql - sql查询保留 "counted"已经删除的行

c# - Entity Framework 和默认值

sql-server - 查找SQLserver登录错误的来源

asp.net - 重写 Entity Framework 中的删除功能