Aakash Sahai
Aakash Sahai

Reputation: 4004

Oracle Query: Making conditional fetch in single query

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.

Upvotes: 3

Views: 411

Answers (3)

dbenham
dbenham

Reputation: 130819

SELECT TI.ID AS "id",
       COALESCE( I.DESCRIPTION, NI.DESCRIPTION ) AS "description"
       COALESCE( I.ID, NI.ID ) AS "itemid",
       TI.QUANTITY AS "quantity"
       COALESCE( I.COL_SUMTHING, NI.COL_SUMTHING ) AS "col_sumthing"
       I.EXTRA_COL AS "extra_col"
  FROM TICKETITEMS TI
  LEFT OUTER JOIN ITEMDETAILS I ON TI.ITEMID = I.ID
  LEFT OUTER JOIN NEWITEMS NI ON I.ID IS NULL AND TI.ITEMID = NI.ID
 WHERE TI.TICKETID = '100'

Upvotes: 2

Mike Meyers
Mike Meyers

Reputation: 2895

You need to work with a UNION of the ITEMDETAILS and NEWITEMS tables but then need to rank the data and only take the relevant rows.

select 
  ti.id, 
  it.description, 
  it.id as itemid, 
  ti.quantity, 
  it.col_sumthing, 
  it.extra_col
from 
  ticketitem ti
  join (
    select 
      id,  
      description,  
      col_sumthing,  
      extra_col,  
      row_number() over (partition by id order by item_ordering) as item_priority
    from (
      select id, description, col_sumthing, extra_col, 1 as item_ordering
      from itemdetails
      union all
      select id, description, col_sumthing, null as extra_col, 2 as item_ordering
      from newitems 
    )
  ) it on (it.id = ti.itemid and it.item_priority = 1)

Upvotes: 0

Vincent Malgrat
Vincent Malgrat

Reputation: 67722

You could use two OUTER joins:

SELECT ti.*, 
       nvl(i.id, n.id) itemid, 
       nvl(i.description, n.description) description, 
       nvl(i.col_sumthing, n.col_sumthing) col_sumthing 
  FROM ticketitems ti
  LEFT JOIN itemdetails i ON ti.itemid = i.id
  LEFT JOIN newsitems n ON ti.itemid = n.id
 WHERE ti.ticket = '100'

This will work as long as both detail tables have ID as a primary key. Use COALESCE if you have more than two detail tables.

Upvotes: 0

Related Questions