[英]How to use Case on join SQL query
i have a query to select with case我有一个查询 select 的案例
select
so.name,
rp.name,
CASE WHEN sp.delivery_type = 'direct' THEN true ELSE false END as is_direct,
CASE WHEN sp.origin ilike 'Return%' THEN true ELSE false END as is_from_return_doc,
jsonb_agg(
json_build_object(
'id', sm.id,
'product_id', pp.id,
'product_sku', pt.default_code,
'product_name', pt.name,
'image_url', CASE WHEN ir.store_fname IS NOT NULL THEN CONCAT('{}', ir.store_fname) ELSE '' END,
'quantity', sm.product_uom_qty,
'uom', json_build_object(
'id', sm_uom.id,
'name', sm_uom.name,
'factor', sm_uom.factor,
'barcode', piu.barcode
)
)
) as items
from sale_order so
JOIN res_partner rp on
so.partner_id = rp.id
JOIN stock_picking sp ON
CASE
WHEN sp.sale_id = so.id and sp.origin = so.name and sp.state in ('done') THEN
sp.sale_id = so.id
WHEN sp.sale_id = so.id and sp.origin ilike 'Return%' and sp.state not in ('cancel', 'done') THEN
sp.sale_id = so.id
END
JOIN stock_move sm on
sm.picking_id = sp.id
JOIN product_product pp on
sm.product_id = pp.id
JOIN product_template pt on
pp.product_tmpl_id = pt.id
JOIN product_uom sm_uom ON
sm.product_uom = sm_uom.id
LEFT JOIN product_identity_uom piu ON
piu.product_id = pt.id
AND piu.uom_id = sm.product_uom
LEFT JOIN ir_attachment ir ON
ir.res_id = pt.id
and ir.res_model = 'product.template'
and ir.res_field = 'image'
WHERE
so.name = 'SO503251'
GROUP BY
so.id, rp.id, sp.id
on this part, i want the last condition is replacing the first condition if the second condition is true, but it always goes to the first condition..在这部分,如果第二个条件为真,我希望最后一个条件替换第一个条件,但它总是转到第一个条件..
so if we found the "return" it will replace the first condition,.. i tried to make it nest condition case in case but didnt work.. please can someone help?因此,如果我们找到“返回”,它将替换第一个条件,.. 我试图让它嵌套条件以防万一但没有用.. 有人可以帮忙吗?
CASE
WHEN sp.sale_id = so.id and sp.origin = so.name and sp.state in ('done') THEN
sp.sale_id = so.id
WHEN sp.sale_id = so.id and sp.origin ilike 'Return%' and sp.state not in ('cancel', 'done') THEN
sp.sale_id = so.id
END
thanks谢谢
I speculate that you want logic something like this:我推测你想要这样的逻辑:
ON sp.sale_id = so.id and
(sp.origin = so.name and sp.state in ('done') or
sp.origin ilike 'Return%' and sp.state not in ('cancel', 'done')
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.