db2 - SQL - Join with default if it's not possible to match -


i have 2 tables this:

table 1:

+---------+---------+-------------+ | activity| area    | responsible | +---------+---------+-------------+ | cooking | meat    |   peter     | | cooking | vegan   |   sia       | | cleaning| kitchen |   paul      | | cleaning| toilets |   selina    | +---------+---------+-------------+ 

table 2:

+---------+---------+-------------+ | activity| area    | day         | +---------+---------+-------------+ | cooking | meat    |   monday    | | cooking | vegan   |   monday    | | cleaning| garden  |   friday    | | cleaning| toilets |   friday    | +---------+---------+-------------+ 

now want sql join them, can see responsible persons each day.

i think standard sql this:

select day, activity, responsible  table_2 2 left join table_1 1  on 1.activity = 2.activity , 1.area = 2.area 

but there rows can not joint (e.g. cleaning garden). in case (if not possible join) want peter responsible it.

can in 1 join (maybe case statement?) or how this?

don't use numbers table names, if db2 allows it. numbers should numbers.

you looking coalesce():

select t2.day, t2.activity, coalesce(t1.responsible, 'peter') responsible  table_2 t2 left join      table_1 t1       on t1.activity = t2.activity , t1.area = t2.area; 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -