souLTower - Thursday, July 12, 2018 11:49 AM
I am getting exactly what your looking for. May be check your explanation and expected results.
with sales (Person,Category,Item,Qty)
as
(
select 'Joe','Books','Title1',3
union all
select 'Mary','Furniture','Couch',0
),
Poss (Person,Category,Item)
as
(
select'Joe','Books','Title1'
union all
select'Joe','Books','Title2'
union all
select'Mary','Books','Title1'
union all
select'Mary','Books','Title2'
union all
select'Joe','Furniture','Couch'
union all
select'Joe','Furniture','Table'
union all
select'Mary','Furniture','Couch'
union all
select'Mary','Furniture','Table'
)
select poss.*, case when sales.item=poss.item then qty else 0 end as qty from
sales
inner join
poss
on sales.Person=poss.Person
order by
poss.person,
poss.Category,
qty desc
Test Results:
Person | Category | Item | qty |
---|
Joe | Books | Title1 | 3 |
Joe | Books | Title2 | 0 |
Joe | Furniture | Couch | 0 |
Joe | Furniture | Table | 0 |
Mary | Books | Title1 | 0 |
Mary | Books | Title2 | 0 |
Mary | Furniture | Couch | 0 |
Mary | Furniture | Table 0 |
Saravanan