SQL Table
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
Expected output (Return components of same price and manufactured yr as distinct combinations):
component component price manufactured
smps fx card 500 2011
case ram 400 2010
cord usb 200 2010
Query tried
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
Result from above query (wrong output though):
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
Please help me eliminating the duplicate combos using the query.
This works if no pairs of components share the same name:
All I changed was to trade the inequality operator
!=for agreater thanoperator. This way you get every pair once instead of twice.To provide for the possibility of identical names:
I assume
idis unique, so it can’t go wrong.