mysql 多重计数并与 WHERE 求和
我正在尝试构建一个查询来检查我的库存。
SELECT COUNT(*) AS item_count,
reseller_id, sum(sold) as
sold_count, sum(refunded) as
refunded_count,**sum(case price when
refunded <> 1 AND sold=1) as pricesum** FROM stock
GROUP BY
reseller_id ORDER BY sold_count ASC
上述查询将选择所有商品,并按每个经销商对它们进行分组,包括商品总数和退款计数。粗体部分是错误的,我想获得总价(这是未退款+每个经销商出售的总价(注意我按经销商 ID 分组)
i'm trying to build a query to check my stock.
SELECT COUNT(*) AS item_count,
reseller_id, sum(sold) as
sold_count, sum(refunded) as
refunded_count,**sum(case price when
refunded <> 1 AND sold=1) as pricesum** FROM stock
GROUP BY
reseller_id ORDER BY sold_count ASC
The above query will select all items and group them by each reseller with total items and refund count. The part in bold is wrong i want to get the total price(which is the sum for the none refunded + sold for each of the resellers (notice i group by reseller id)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的 case 语句可能会起作用:
Something like this with the case statements might work:
我注意到的事情:
Things that I noticed:
尝试创建如下子查询:
SELECT SUM(price) FROM stock WHERE returneded != 1 AND sell = 1
完整的查询如下所示:
Try to make a subquery like:
SELECT SUM(price) FROM stock WHERE refunded != 1 AND sold = 1
The complete query would look like this: