假设我有一张物料清单表,如下所示:
+----------+-------------+
| ParentNo | ComponentNo |
+----------+-------------+
| AAA | 101 |
| AAA | 102 |
| BBB | 201 |
| BBB | 202 |
| CCC | 101 |
| CCC | 201 |
| CCC | 301 |
| DDD | 101 |
| DDD | 102 |
+----------+-------------+
我想编写一个查询,找到共享相同组件集的任何父数字。我对输出很灵活,但我最初的想法是它类似于:
+----------+---------+
| ParentNo | Matched |
+----------+---------+
| AAA | DDD |
| DDD | AAA |
+----------+---------+
如Finding duplicate values in a SQL table中所述,有多种方法可以查找为一个或多个字段共享相同值的单个记录。但是我只想在 all 两个不同父项的组件匹配时返回结果。
我见过的最接近的解决方案是SQL selecting rows where one column's value is common across another criteria column。这不符合我的需要,因为必须提供一组特定的ComponentNo。我试图避免手动输入每个ParentNo的ComponentNo(我正在使用的数据集包含数千万条记录)。
我觉得某种形式的递归查询在这种情况下可能是合适的,但是我的尝试并没有产生任何看起来像是在正确路径上的查询。
答案 0 :(得分:0)
您可以使用LISTAGG
连接父级的所有组件。然后使用self join
检查具有相同组件的父项。
with grouped_comps as (
select parentno, LISTAGG(cast(componentno as varchar(10000)), ',') WITHIN GROUP(ORDER BY componentno) as comp_all
from t
group by parentno)
select g1.parentno,g2.parentno
from grouped_comps g1
join grouped_comps g2 on g1.parentno<>g2.parentno and g1.comp_all=g2.comp_all
答案 1 :(得分:0)
尝试类似这样的事情
<unknown>:0: error: no such file or directory: '/Users/trevorjordy/Library/Developer/Xcode/DerivedData/Duelyst_Database-eiwmunfsyfyouhbystikkuvfadfh/Build/Intermediates/Duelyst Database.build/Debug-iphonesimulator/Duelyst Database.build/DerivedSources/CoreDataGenerated/Model/.NewestCardsEntity+CoreDataClass.swift'
<unknown>:0: error: no such file or directory: '/Users/trevorjordy/Library/Developer/Xcode/DerivedData/Duelyst_Database-eiwmunfsyfyouhbystikkuvfadfh/Build/Intermediates/Duelyst Database.build/Debug-iphonesimulator/Duelyst Database.build/DerivedSources/CoreDataGenerated/Model/.NewestCardsEntity+CoreDataProperties.swift'
<unknown>:0: error: no such file or directory: '/Users/trevorjordy/Library/Developer/Xcode/DerivedData/Duelyst_Database-eiwmunfsyfyouhbystikkuvfadfh/Build/Intermediates/Duelyst Database.build/Debug-iphonesimulator/Duelyst Database.build/DerivedSources/CoreDataGenerated/Model/.SetsEntity+CoreDataClass.swift'
<unknown>:0: error: no such file or directory: '/Users/trevorjordy/Library/Developer/Xcode/DerivedData/Duelyst_Database-eiwmunfsyfyouhbystikkuvfadfh/Build/Intermediates/Duelyst Database.build/Debug-iphonesimulator/Duelyst Database.build/DerivedSources/CoreDataGenerated/Model/.SetsEntity+CoreDataProperties.swift'
Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1
答案 2 :(得分:0)
另一种可能性:
with data (bk,komp) as (values
('AAA', 101),('AAA', 102),('BBB', 201),
('BBB', 202),('CCC', 101),('CCC', 201),
('CCC', 301),('DDD', 101),('DDD', 102)
)
select d1.bk,d2.bk
from data d1
inner join data d2
on d1.bk <> d2.bk
group by d1.bk,d2.bk
having count(distinct d1.komp) =
count(case when d1.komp = d2.komp then 1 end)
答案 3 :(得分:-1)
SELECT Parent.ParentNo,Child.ParentNo FROM
TABLE AS Parent
LEFT JOIN TABLE as Child
ON Child.ComponentNo = Parent.ComponentNo
Where Parent.ParentNo != Child.ParentNo