SQL中列值组合的筛选器
我想筛选与其他人在某些属性上具有相同AttributeValue的所有人 我有以下疑问:SQL中列值组合的筛选器,sql,sql-server,tsql,Sql,Sql Server,Tsql,我想筛选与其他人在某些属性上具有相同AttributeValue的所有人 我有以下疑问: SELECT p1.keyValue, p1.Displayname, p2.keyValue, p2.Displayname, p1.ImportantAttrName, p1.ImportantAttrValue FROM Person p1 WITH (NOLOCK) JOIN Person p2 WITH (NOLOCK) ON p1.ImportantAttr = p2
SELECT
p1.keyValue,
p1.Displayname,
p2.keyValue,
p2.Displayname,
p1.ImportantAttrName,
p1.ImportantAttrValue
FROM Person p1 WITH (NOLOCK)
JOIN Person p2 WITH (NOLOCK)
ON p1.ImportantAttr = p2.ImportantAttr
WHERE p1.keyValue != p2.keyValue
AND p1.ImportantAttrValue = p2.ImportantAttrValue
通过这个查询,我将两次获得所有条目,因为每个人都在p1和p2中。
结果如下所示:
I123 Freddy Krüger A123 The Horsemen Moviecategorie Horror
A123 The Horsemen I123 Freddy Krüger Moviecategorie Horror
SELECT
p1.keyValue,
p1.Displayname,
p2.keyValue,
p2.Displayname,
p1.ImportantAttrName,
p1.ImportantAttrValue
FROM Person p1 WITH (NOLOCK)
JOIN Person p2 WITH (NOLOCK)
ON p1.ImportantAttr = p2.ImportantAttr
AND p1.keyValue < p2.keyValue
WHERE p1.ImportantAttrValue = p2.ImportantAttrValue
但是出于分析的目的,如果我可以只得到一次p1.keyvalue和p2.keyvalue的组合,而不考虑这两个列中的哪个列是值,那就太好了
到目前为止,我是通过导出到excel并在那里进行清理来做到这一点的,但是有没有办法修复查询以不获取此“重复项”使用
其中p1.keyValue
:
SELECT
p1.keyValue,
p1.Displayname,
p2.keyValue,
p2.Displayname,
p1.ImportantAttrName,
p1.ImportantAttrValue
FROM Person p1 WITH (NOLOCK)
INNER JOIN Person p2 WITH (NOLOCK)
ON p1.ImportantAttr = p2.ImportantAttr
WHERE
p1.keyValue < p2.keyValue AND -- change is here
p1.ImportantAttrValue = p2.ImportantAttrValue;
选择
p1.1键值,
p1.显示名称,
p2.keyValue,
p2.显示名称,
p1.importanterName,
p1.IMPORTATTRVALUE
来自具有(NOLOCK)的人员p1
内部连接人员p2与(NOLOCK)
在p1.ImportantAttr=p2.ImportantAttr上
哪里
p1.keyValue
这将确保您不会看到重复的对。为了理解这是为什么,请考虑两个关键值:<代码> 1 < /代码>和<代码> 2 < /代码>。使用条件
=代码>,则1-2
和2-1
都符合该标准。但是使用使用其中p1.keyValue
:
SELECT
p1.keyValue,
p1.Displayname,
p2.keyValue,
p2.Displayname,
p1.ImportantAttrName,
p1.ImportantAttrValue
FROM Person p1 WITH (NOLOCK)
INNER JOIN Person p2 WITH (NOLOCK)
ON p1.ImportantAttr = p2.ImportantAttr
WHERE
p1.keyValue < p2.keyValue AND -- change is here
p1.ImportantAttrValue = p2.ImportantAttrValue;
选择
p1.1键值,
p1.显示名称,
p2.keyValue,
p2.显示名称,
p1.importanterName,
p1.IMPORTATTRVALUE
来自具有(NOLOCK)的人员p1
内部连接人员p2与(NOLOCK)
在p1.ImportantAttr=p2.ImportantAttr上
哪里
p1.keyValue
这将确保您不会看到重复的对。为了理解这是为什么,请考虑两个关键值:<代码> 1 < /代码>和<代码> 2 < /代码>。使用条件=代码>,则1-2
和2-1
都符合该标准。但使用可以:
on p1.ImportantAttr = p2.ImportantAttr
致:
p1.ImportantAttr=p2.ImportantAttr和p1.keyValue
整个查询可能如下所示:
I123 Freddy Krüger A123 The Horsemen Moviecategorie Horror
A123 The Horsemen I123 Freddy Krüger Moviecategorie Horror
SELECT
p1.keyValue,
p1.Displayname,
p2.keyValue,
p2.Displayname,
p1.ImportantAttrName,
p1.ImportantAttrValue
FROM Person p1 WITH (NOLOCK)
JOIN Person p2 WITH (NOLOCK)
ON p1.ImportantAttr = p2.ImportantAttr
AND p1.keyValue < p2.keyValue
WHERE p1.ImportantAttrValue = p2.ImportantAttrValue
选择
p1.1键值,
p1.显示名称,
p2.keyValue,
p2.显示名称,
p1.importanterName,
p1.IMPORTATTRVALUE
来自具有(NOLOCK)的人员p1
将p2与(NOLOCK)连接
在p1.ImportantAttr=p2.ImportantAttr上
p1.keyValue
您可以打开:
on p1.ImportantAttr = p2.ImportantAttr
致:
p1.ImportantAttr=p2.ImportantAttr和p1.keyValue
整个查询可能如下所示:
I123 Freddy Krüger A123 The Horsemen Moviecategorie Horror
A123 The Horsemen I123 Freddy Krüger Moviecategorie Horror
SELECT
p1.keyValue,
p1.Displayname,
p2.keyValue,
p2.Displayname,
p1.ImportantAttrName,
p1.ImportantAttrValue
FROM Person p1 WITH (NOLOCK)
JOIN Person p2 WITH (NOLOCK)
ON p1.ImportantAttr = p2.ImportantAttr
AND p1.keyValue < p2.keyValue
WHERE p1.ImportantAttrValue = p2.ImportantAttrValue
选择
p1.1键值,
p1.显示名称,
p2.keyValue,
p2.显示名称,
p1.importanterName,
p1.IMPORTATTRVALUE
来自具有(NOLOCK)的人员p1
将p2与(NOLOCK)连接
在p1.ImportantAttr=p2.ImportantAttr上
p1.keyValue
这可能是不同的方法,但可以达到预期效果
使用分区计数(*):
根据上面的查询,您将得到如下结果
> RepeatCount keyValue DisplayName Attr
>
> 1 P321 The Ironman Generalcategorie Test
> 2 I123 Freddy Krüger Moviecategorie Horror
> 2 A123 The Horsemen Moviecategorie Horror
根据该结果,您可以通过Repeatcount>1筛选记录,这可能是不同的方法,但可以得到预期的结果
使用分区计数(*):
根据上面的查询,您将得到如下结果
> RepeatCount keyValue DisplayName Attr
>
> 1 P321 The Ironman Generalcategorie Test
> 2 I123 Freddy Krüger Moviecategorie Horror
> 2 A123 The Horsemen Moviecategorie Horror
从这个结果中,您可以通过Repeatcount>1arg facepalm筛选记录…有时它真的是“为树而失林”。。。。谢谢,我会接受这个答案,因为它也解释了为什么这个工作组facepalm…有时候它真的是“为树怀念森林”。。。。谢谢,我会接受这个答案,因为它也解释了为什么这样做