WITH RECURSIVE categorys AS (
SELECT
pc.id
FROM nt_product_category pc
where case when cast(%(category_id)s as text)='0' then Falseelse pc.parent_id in%(category_id)s or pc.idin%(category_id)s end
union ALL
SELECT
pc1.id
FROM categorys c
inner join nt_product_category pc1 on pc1.parent_id = c.id)
select nss.code as shop_code,nss.name as shop_name,np.code as product_code,np.name as product_name,npvc.color_name,npvs.size_name,line.wait_stock_count,line.stock_count,line.stock_count*np.sale_price as stock_amount,line.sale_count,line.sale_amount
from(
select al.product_id,al.shop_id,sum(al.wait_stock_count)as wait_stock_count,sum(al.stock_count)as stock_count,sum(al.sale_count)as sale_count,sum(al.sale_amount)as sale_amount
from(
select product_id,shop_id,COALESCE(sum(transit_count),0.00)as wait_stock_count,0as stock_count,0as sale_count,0as sale_amount
from(
SELECT
nsol.product_id,
nso.shop_id,0as wait_stock_count,0as stock_count,
COALESCE(SUM(nsol.quantity),0) sale_count,
COALESCE(ROUND(SUM(nsol.amount_after_discount),2),0) sale_amount
FROM
nt_sale_order_line nsol
JOIN
nt_sale_order nso ON nsol.order_id = nso.id
left join nt_product np
on np.id= nsol.product_id
left join nt_product np2
on np2.id= np.parent_id
WHERE
nso.shop_id in%(shop_ids)s AND to_char(nso.order_day,'YYYY-MM-DD')>=%(start_date)s AND to_char(nso.order_day,'YYYY-MM-DD')<=%(end_date)s AND nso.state ='done'and case when cast(%(product_ids)s as text)<>'0' then (case when np.parent_id isnot null then np2.id IN %(product_ids)s else np.id IN %(product_ids)s end)elseTrue end
and case when cast(%(year)s as text)<>'0' then (case when np.parent_id isnot null then np2.year in%(year)s else np.year in%(year)s end)elseTrue end
and case when cast(%(season)s as text)<>'0'then (case when np.parent_id isnot null then np2.season in%(season)s else np.season in%(season)s end)elseTrue end
and case when cast(%(sex)s as text)<>'0' then (case when np.parent_id isnot null then np2.sex in%(sex)s else np.sex in%(sex)s end)elseTrue end
and case when cast(%(wave)s as text)<>'0' then (case when np.parent_id isnot null then np2.wave in%(wave)s else np.wave in%(wave)s end)elseTrue end
and case when cast(%(series)s as text)<>'0' then (case when np.parent_id isnot null then np2.series in%(series)s else np.series in%(series)s end)elseTrue end
and case when cast(%(bigclass)s as text)<>'0' then (case when np.parent_id isnot null then np2.bigclass in%(bigclass)s else np.bigclass in%(bigclass)s end)elseTrue end
and case when cast(%(subclass)s as text)<>'0' then (case when np.parent_id isnot null then np2.subclass in%(subclass)s else np.subclass in%(subclass)s end)elseTrue end
and case when cast(%(settle_class)s as text)<>'0' then (case when np.parent_id isnot null then np2.settle_class in%(settle_class)s else np.settle_class in%(settle_class)s end)elseTrue end
and case when cast(%(style_attribute)s as text)<>'0' then (case when np.parent_id isnot null then np2.style_attribute in%(style_attribute)s else np.style_attribute in%(style_attribute)s end)elseTrue end
and case when cast(%(category_id)s as text)<>'0' then (case when np.parent_id isnot null then np2.category_id in(select idfrom categorys group by id)else np.category_id in(select idfrom categorys group by id) end)elseTrue end
GROUP BY
nso.shop_id, nsol.product_id
)as al
group by al.product_id,al.shop_id
)as line
left join nt_product np
on np.id= line.product_id
left join nt_sale_shop nss
on nss.id= line.shop_id
left join (
select ppvvr.product_id,npvo.name as color_name from product_product_variant_value_rel ppvvr
join nt_product_variant_value npvv
on npvv.id= ppvvr.value_id
join nt_product_variant_type npvt
on (npvt.id= npvv.type_id and npvt.type='color')
join nt_product_variant_option npvo
on (npvo.id= npvv.option_id and npvo.type_id = npvt.id)) npvc
on npvc.product_id = np.id
left join (
select ppvvr.product_id,npvo.name as size_name from product_product_variant_value_rel ppvvr
join nt_product_variant_value npvv
on npvv.id= ppvvr.value_id
join nt_product_variant_type npvt
on (npvt.id= npvv.type_id and npvt.type='size')
join nt_product_variant_option npvo
on (npvo.id= npvv.option_id and npvo.type_id = npvt.id)) npvs
on npvs.product_id = np.id
where line.wait_stock_count <>0or line.stock_count <>0or line.sale_count <>0or sale_amount <>0
order by nss.code,np.code
解析:源码中:
FROM
nt_sale_order_line nsol
JOIN
nt_sale_order nso ON nsol.order_id = nso.id
left join nt_product np
on np.id= nsol.product_id
left join nt_product np2
on np2.id= np.parent_id
and case when cast(%(product_ids)s as text)<>'0' then (case when np.parent_id isnot null then np2.id IN %(product_ids)s else np.id IN %(product_ids)s end)elseTrue end
3.在查询语句中加入以上逻辑:创建一个商品列表,包含了sku_id或spu_id
product_list as(
select idfrom nt_product where parent_id in%(product_ids)s
union
select idfrom nt_product where idin%(product_ids)s
)