如何重构此 select 语句以删除重复
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;
是否存在没有连接的简单选项,也许像 with
语句可以在 select 中使用?
注意
项目有多个条目,条目有一个产品
最佳答案
可以改用 CTE 吗?
;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;
关于sql - 在 SELECT 语句中的 CASE 中重构 SELECT 的重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57877577/