我有一个这样的表:
"Order1", "Product1", "3"
"Order1", "product2", "5"
"Order2", "product1", "2"
依此类推,我想要一个输出类似以下内容的查询:
"Order1", "Product1", "3/8"
"Order1", "product2", "5/8"
"Order2", "product1", "2/n"
8是每个订单上产品总数的总和。
有任何想法吗?谢谢!
最佳答案
这可以为您工作:
SELECT s.ordernr
,p.product
,CONCAT(p.quantity, '/', sum(s.quantity) AS percentage
FROM yourtable s
LEFT JOIN yourtable p ON p.orderid = s.oerderid
GROUP BY s.ordernr
ORDER BY s.ordernr;