how to refactor this select statement to remove duplication
select ( case when ( select my_product_int_attribute -- duplication from app_public.products where product_id = entry.product_id ) > 0 then 1 when ( select my_product_int_attribute -- duplication from app_public.products where product_id = entry.product_id ) = 0 then 0 else null end ) from app_public.entries as entry where entry.project_id = 1;
are there exists easy options without joins, maybe something like with
statements that can be used inside select?
N.B.
project has many entries, entry has one product
Advertisement
Answer
May be use CTE instead?
;with products as ( select products.product_id, products.my_product_int_attribute from app_public.products group by products.product_id, products.my_product_int_attribute ) select ( case when products.my_product_int_attribute > 0 then 1 when products.my_product_int_attribute = 0 then 0 else null end ) from app_public.entries as entry left join products on products.product_id = entry.product_id where entry.project_id = 1;