id component price manufactured
1 fx card 500 2011
2 ram 400 2010
3 case 400 2010
4 smps 500 2011
5 cord 200 2010
6 usb 200 2010
预期产出(返回相同价格的组件,并按不同的组合制造):
component component price manufactured
smps fx card 500 2011
case ram 400 2010
cord usb 200 2010
SELECT m1.[component]
,m2.[component]
,m1.[price]
,m1.[manufactured]
FROM [dbo].[Mfg] m1
inner join [dbo].[Mfg] m2
on m1.component != m2.component
and m1.price = m2.price
and m1.manufactured = m2.manufactured
来自上述查询的结果(但输出错误):
component component price manufactured
smps fx card 500 2011
case ram 400 2010
cord usb 200 2010
ram case 400 2010
fx card smps 500 2011
usb cord 200 2010
请帮我删除使用查询的重复组合。
答案 0 :(得分:0)
如果没有任何组件对共享相同名称,则此方法有效:
SELECT m1.[component]
,m2.[component]
,m1.[price]
,m1.[manufactured]
FROM [dbo].[Mfg] m1
JOIN [dbo].[Mfg] m2 ON m1.component > m2.component
AND m1.price = m2.price
AND m1.manufactured = m2.manufactured;
我改变的只是为!=
运算符交换不等运算符greater than
。这样你就可以得到每一对而不是两次。
提供相同名称的可能性:
JOIN [dbo].[Mfg] m2 ON m1.id > m2.id
AND m1.price = m2.price
AND m1.manufactured = m2.manufactured;
我认为id
是唯一的,所以它不会出错。