gpt4 book ai didi

MySql SELECT,根据另一列对一列求和

转载 作者:行者123 更新时间:2023-11-29 21:40:46 25 4
gpt4 key购买 nike

这是我的 table

sku   |  type   |  qty
abc order 3
abc refund 1
abc order 5
def order 2
def refund 1

我想获取type为“订单”的sku的数量,并扣除type为“退款”的数量。

因此,sku“abc”的预期数量为 7,“def”的预期数量为 1

我的查询没有按预期执行。

select sku, type, sum(qty) 
from table
group by sku
having type ='order'

最佳答案

选择 货号, 类型, sum(CASE WHEN type = "order"THEN qty ELSE -qty END) AS sum 总和从表按 sku 分组

关于MySql SELECT,根据另一列对一列求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34538935/

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