• souLTower - Thursday, July 12, 2018 11:49 AM

    If you were to use inner join and the Mary sales record had 2 entries, each entry would join to the POSS table causing duplication.  I don't think inner join is the way to go.

    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:

    Saravanan