I have three tables in oracle db as newitems, itemdetails, ticketitems table.
Some dummy data as follow:
TICKETITEMS:
id ticketid itemid quantity
1 100 9999 2
2 100 9998 5
3 100 2222 3
ITEMDETAILS:
id description col_sumthing extra_col
9999 Marlboro val_sumthing 123_op
9998 Cigar Black val_sumthing 456_pqwe
NEWITEMS:
id description col_sumthing
2222 100Pipes val_different
Initially i had to fetch data only from itemdetails + ticketitems which was very easy using simple joins. Query for which was:
SELECT "TI".*, "I"."ID" AS "ITEMID", "I"."DESCRIPTION", "I"."col_sumthing"
FROM "TICKETITEMS" "TI"
INNER JOIN "ITEMDETAILS" "I" ON TI.ITEMID = I.ID
WHERE (TI.TICKET = '100')
Something like that.
Now newitem table introduced which may also have some items present in ticketitems table.
So i want a result like:
Final Result:
id description itemid quantity col_sumthing extra_col
1 Marlboro 9999 2 val_sumthing 123_op
2 Cigar Black 9998 5 val_sumthing 456_pqwe
3 100Pipes 2222 3 val_different
The problem I m facing is, it should only check in NEWITEMS when no details found in itemdetails. Any other work around is also welcomed.
You could use two OUTER joins:
This will work as long as both detail tables have
IDas a primary key. UseCOALESCEif you have more than two detail tables.