postgresql - Setting value using a subquery that return's no row on Postgres -


i have 2 tables: tire_life , transaction on database.

it works fine when subquery returns value, when no row returned not update tire_life table.

that's query i'm running.

update tire_life life set covered_distance = initial_durability + transactions.durability  (select tire_life_id, sum(tire_covered_distance) durability              transaction tire_life_id = 24 , deleted_at null        group (tire_life_id)) transactions  life.id = 24 

i have tried use coalesce() function no success @ all.

you can use coalesce() subquery if returns single column. not use tire_life_id outside subquery, can skip it:

update tire_life life  set covered_distance = initial_durability + transactions.durability  (     select coalesce(         (             select sum(tire_covered_distance)             transaction              tire_life_id = 24 , deleted_at null              group (tire_life_id)         ), 0) durability     ) transactions  life.id = 24; 

i guess want 0 durability if subquery returns no rows.


Comments