Let's say I have this following query:
select
*,
case when x in ('x','y','z',...) then 'X' end as value_x
from(
select *,
case when condition1 = true
case
when field2 == 'xxxx' then substr(field2, 0, 2)
when field2 == 'yyyy' then substr(field2, 0, 2)
when field2 == 'yxyx' then substr(field2, 0, 2)
...
end
end as value_x
from table) as subquery;
When I do this, I end up getting 2 columns named value_x
. Is there any way that I can filter value_x
with the IN function without having to use it in every single case? There are a lot of cases, and it would really bloat up the query.
The query-subquery structure can't be changed, as there are dozens of those cases that use values from the subquery to create new columns, this is the first time that I have to update one of the columns coming from the subquery, and I can only use a SELECT
statement.
I'm using PostgreSQL.