gpt4 book ai didi

mysql - SQL:语句仅返回第一行的小计

转载 作者:行者123 更新时间:2023-11-29 10:49:09 25 4
gpt4 key购买 nike

我有如下示例数据:
Sample Data

我使用下面的 SQL 语句:

select a.product_category_id, b.Favorite, c.In_Cart,d.Pre_sales_order,
sum(b.Favorite)+sum(c.In_Cart)+sum(d.Pre_sales_order) as SubTotal
from
(select distinct product_category_id
from item_activity
where last_item_status_code in (6,7,8)
) a
left join
(select product_category_id, count(last_item_status_code) as Favorite
from .item_activity
where last_item_status_code='6'
group by product_category_id
) b on a.product_category_id=b.product_category_id
left join
(select product_category_id, count(product_category_id) as In_Cart
from item_activity
where last_item_status_code='7'
group by product_category_id
) c on c.product_category_id=a.product_category_id
left join
(select product_category_id, count(product_category_id) as Pre_sales_order
from item_activity
where last_item_status_code='8'
group by product_category_id
) d on d.product_category_id=a.product_category_id
group by a.product_category_id
;

并实现了这一点:
result

但它只是给我第一行的小计......

最佳答案

试试这个:

select product_category_id,
sum(case when last_item_status_code=6 then 1 else 0 end) As Favorite,
sum(case when last_item_status_code=7 then 1 else 0 end) As In_Cart,
sum(case when last_item_status_code=8 then 1 else 0 end) As Pre_sales_order,
count(last_item_status_code) as SubTotal
from item_activity
where last_item_status_code in (6,7,8)
group by product_category_id;

关于mysql - SQL:语句仅返回第一行的小计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44060362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com