admin管理员组文章数量:1287569
I am having a table as below, where I am receiving products for each PO, multiple times and need to Get Total Quantity received as per their Respective PO and order them as per Product Name
What I want is
I tried below SQL query:
SELECT Product_Name, PONo , sum(Quantity) AS Total_Quantity
FROM GrnTable
GROUP BY Product_Name;
but it is summing up all quantities, from all PO Number.
I am having a table as below, where I am receiving products for each PO, multiple times and need to Get Total Quantity received as per their Respective PO and order them as per Product Name
What I want is
I tried below SQL query:
SELECT Product_Name, PONo , sum(Quantity) AS Total_Quantity
FROM GrnTable
GROUP BY Product_Name;
but it is summing up all quantities, from all PO Number.
Share Improve this question edited Mar 1 at 6:03 Dale K 27.4k15 gold badges58 silver badges83 bronze badges asked Feb 24 at 14:53 leenaleena 131 silver badge2 bronze badges 3 |2 Answers
Reset to default 2I would suggest grouping by the PO number first, and then also group by the Product Name : Group By PONo., Product Name
By adding the group by to the PONo, you accomplish the first task of grouping by respective PO.
SELECT Product as Product_Name, PONo , sum(Quantity) AS Total_Quantity
FROM GrnTable
GROUP BY PONo,Product;
EX:
TestTable
PONo | Product | Quantity |
---|---|---|
PO-18 | Booster | 10 |
PO-18 | Booster | 10 |
PO-18 | Booster | 20 |
PO-18 | Booster | 20 |
PO-19 | Booster | 10 |
PO-19 | Booster | 15 |
PO-20 | Booster | 20 |
PO-20 | Booster | 25 |
PO-18 | Booster | 5 |
PO-18 | Booster | 15 |
PO-18 | Bottle Cap | 25 |
PO-18 | Bottle Cap | 5 |
TestResults
Product_Name | PONo | Total_Quantity |
---|---|---|
Booster | PO-18 | 80 |
Booster | PO-19 | 25 |
Booster | PO-20 | 45 |
Bottle Cap | PO-18 | 30 |
SELECT
Product_Name,
PONo,
SUM(Quantity) AS Total_Quantity
FROM GrnTable
GROUP BY 1, 2
本文标签: sqlMySQL Get the sum of Same Product from Same PO and order them as per Product NameStack Overflow
版权声明:本文标题:sql - MySQL Get the sum of Same Product from Same PO and order them as per Product Name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741260438a2367517.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
group by
takes a list of expressions; you want everything with the same PONo and Product_Name grouped together so say that:group by PONo, Product_Name
– ysth Commented Feb 24 at 14:57