sql - How to write postgres query for below issue? -
level1 | level2 | level3 | level4 key | value | key |value | key | value | key | value ---------------------|----------------|---------------------|----------------- setting1 | true | | setting1 | true |setting1 | false |setting2 | false| |setting2 | false | | |setting3 | true
i have 4 tables. level1, level2, level3, level4. each has key value pair.
i want key, value pair such if key value pair present in level1 should not check level2,3,4.
if it's not present in level1 should go level2 if not level3.
value present in level4.
so final o/p of type map.
key | value --------------------- setting1 | true setting2 | false setting3 | true
is possible sql query or need write function or procedure it?
one approach use case - when structure.
see sqlfiddle
select case when t1."key" not null t1."key" else case when t2."key" not null t2."key" else case when t3."key" not null t3."key" else t4."key" end end end "key", case when t1."value" not null t1."value" else case when t2."value" not null t2."value" else case when t3."value" not null t3."value" else t4."value" end end end "value" table4 t4 left join table1 t1 on t4."key" = t1."key" left join table2 t2 on t4."key" = t2."key" left join table3 t3 on t4."key" = t3."key"
Comments
Post a Comment