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
  • why have you not shown what you got ?, ergo - show what you got , tks, additionally, do not post images - totally useless if we want to copy/re-create, simple copy/paste of textual data is all that is needed. – ticktalk Commented Feb 24 at 14:57
  • 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
  • As per the question guide, please do not post images of code, data, error messages, etc. - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. – Dale K Commented Mar 1 at 6:03
Add a comment  | 

2 Answers 2

Reset to default 2

I 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