postgres update with if condition query

Solutions on MaxInterview for postgres update with if condition query by the best coders in the world

showing results for - "postgres update with if condition query"
Solal
19 Sep 2020
1update t1
2        set t1.current_location =   
3        CASE  
4			WHEN SUBQUERY.status = 0 THEN 'Deployed' 
5			WHEN SUBQUERY.status = 1 THEN 'Retrieved' 
6			WHEN SUBQUERY.status = 2 THEN 'Lost' 
7            ELSE t1.current_location
8		END 
9from (
10    select t3.serial_number, t2.status
11    from t2 inner join t3
12        on t2.some_id = t3.some_id
13    ) as SUBQUERY
14
15where SUBQUERY.serial_number = t1.serial_number;
Martín
24 Jul 2017
1update t1 
2  set current_location = 
3    case t2.status
4        when 0 then 'Deployed'
5        when 1 then 'Retrieved'
6        when 2 then 'Lost'
7    end
8from t2 inner join t3
9   on t2.some_id = t3.some_id
10where t3.serial_number = t1.serial_number;