Skip to content
Advertisement

Selecting default row if no rows return with join condition values from other tables

I have two tables ORDERS and RATE:

and I would like to get the costs of all the orders in table ORDERS, given that a default rate of 10 is used for those orders which do not have rates in the RATE table for the corresponding ids, and if an id is available in the RATE table, but no rate for a certain dow of that id is available, a default rate with dow 0 is always available for that id.

For the above example, the following would be expected:

Note that the 350 for id=1 and dow = 3 comes from count 7 multiplied by rate 50 for id=1 and dow=0, as there is no rate for id=1 and dow=3 in the table. I have tried the following with no luck:

and the following has compilation error:

ORA-00904: “O”.”DOW”: invalid identifier

  1. 00000 – “%s: invalid identifier”

Can someone please help with the query?

Advertisement

Answer

You can use multiple outer joins with RATE table to fetch the desired result as following:

Cheers!!

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement