So optimized a sql server proc recently. Went from this
select id, 'col1' as valtype, col1 as val from table1 where col1 > 0
union all
select id, 'col2', col2 from table1 where col2 > 0
union all
select id, 'col3', col3 from table1 where col3 > 0
to this:
select * from (
select id, valtype,
case valtype
when 'col1' then col1
when 'col2' then col2
when 'col3' then col3
end as val
from table1
cross join (VALUES('col1'),('col2'),('col3')) t(valtype)
) a where val > 0
to this:
select * from (
select id, value as valtype,
choose(ordinal, col1, col2, col3) as val
from table1
cross join string_split('col1,col2,col3', ',', 1) t
) a where val > 0
So how would I go about this progression in Postgres?
Is there an equivalent function to CHOOSE other than CASE?
Is there a string_to_array that also returns index?
2
Answers
I don’t think there is a postgresql equivalent of
CHOOSE()
, however something likestring_split
can be done withunnest with ordinality
andstring_to_array
like follows:If you find the
choose()
function convenient and desirable, you can easily create it in Postgres:Instead of
string_split()
usestring_to_table() with ordinality
:Test it in db<>fiddle.