Home > Enterprise >  Compare double ids, group into one
Compare double ids, group into one

Time:11-16

I have a table "stats" that consists 3 ids and qty.

From this table, I want to return id_part, id_proj and qty.

enter image description here

The solution is partly here, the only problem is that when the ids are double, it should show only one row with summed qty.

For example:

P1, J4, 1700

P3, J1, 600

P3, J4, 1700

SELECT id_part, id_proj, qty
FROM stats
ORDER BY id_del, id_proj;

enter image description here

I have tried with case, also I somehow wanted to compare first row with second, but no luck.

CodePudding user response:

Use Group By with sum(qty) on id_part to get a single result.

Code:

SELECT id_part, id_proj, sum(qty)
FROM stats
Group BY id_part
ORDER BY id_part, id_proj;
  •  Tags:  
  • sql
  • Related