I have the following tables:
customers table:
name | |
---|---|
saqib | [email protected] |
scott | [email protected] |
amy | [email protected] |
account_description table:
account_type | description |
---|---|
13 | personal |
14 | corporate |
Is there way to re-write the following query without CTE or subquery?
with cte_customers as (
select
case
when email ilike '%gmail.com' or email ilike '%hotmail.com' or email ilike '%yahoo.com' then 13
else 14
end as account_type_generated
, name
from customers
)
select *
from cte_customers
inner join account_description on account_type_generated = account_description.account_type ;
SQL Fiddle:
https://www.db-fiddle.com/f/c5KCj5sCcJXmYxsHDCNGAF/0
2
Answers
Simply add the join and case WHen to the SELECT that you have in the CTE
Query #1
Query #2
View on DB Fiddle
just put the CASE into the ON:
but then given the
account_type_generated
is duplicate, it could be drop:and the CASE could be rewritten as an IFF
This is all on Snowflake.