My situation is this. I have a table of products with a pk "Parent" which has "Components" The data looks something like this
Parent(PK) Component
Car1 Wheel
Car1 Tyre
Car1 Roof
Car2 Alloy
Car2 Tyre
Car2 Roof
Car3 Alloy
Car3 Tyre
Car3 Roof
Car3 Leather Seats
Now what I want to do is some query that I can feed two codes in and see the differences... IE If I feed in "Car1", "Car2" it would return something like;
Parent Component
Car1 Wheel
Car2 Alloy
As this is the difference between the two. If I said "Car1", "Car3" I would expect;
Parent Component
Car1 Wheel
Car3 Alloy
Car3 Leather Seats
Your help with this matter would be greatly appreciated.
Without GROUP BY or UNION:
create table Products (
Parent varchar(20) not null,
Component varchar(20) not null
)
insert into Products (Parent,Component)
select 'Car1','Wheel' union all
select 'Car1','Tyre' union all
select 'Car1','Roof' union all
select 'Car2','Alloy' union all
select 'Car2','Tyre' union all
select 'Car2','Roof' union all
select 'Car3','Alloy' union all
select 'Car3','Tyre' union all
select 'Car3','Roof' union all
select 'Car3','Leather Seats'
go
select
ISNULL (a.Parent,b.Parent) as Parent,
ISNULL (a.Component,b.Component) as Component
from
Products a
full outer join
Products b
on
a.Component = b.Component and
a.Parent = 'Car1' and
b.Parent = 'Car3'
where
(a.Parent = 'Car1' and b.Parent is null) or
(b.Parent = 'Car3' and a.Parent is null)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With