I am using Snowflake, and I have a couple of old tables that I am using to generate a new table. The new table has to have the same column names as the old tables. To make the new table, I do a join. Simplified a little, the SQL looks like this -
create table NEW_TABLE as
select
f(A.abc, B.abc) as abc
from OLD_TABLE A
join OTHER_TABLE B on <conditions>
where f(A.abc, B.abc) > 0
It works. But the where part is not good because I have to recompute f(A.abc, B.abc). If I alias abc as something else like select f(A.abc, B.abc) as abc2 I can simply reference abc2 in the where. However, then NEW_TABLE will have a column named abc2 which is wrong. If I don't alias abc, then anytime I reference abc, it uses table A's value for abc instead of the newly computed one. What is the right solution here?