Skip to content
Advertisement

Summing up the rows

enter image description here

I want to add up the profit of all the rows where 1.a=2.a && 1.b=2.b && 1.c=2.c ( The case in first and second row , also in third and fourth ). I wrote the following query but this isn’t working .

db.Execute “UPDATE a,b,c,SUM(profit) FROM Sum ORDER BY a”

Query execution gives an error. Here is a sample data, be it six records with four fields each.

ID | a | b | c | d
==================
   | 1 | 1 | 1 | 50 
   | 1 | 1 | 1 |100 
   | 1 | 2 | 3 | 54 
   | 1 | 2 | 3 | 46 
   | 1 | 2 | 4 | 50 
   | 1 | 1 | 2 |100

The expected result set is 4 records with last 2 being the same

ID | a | b | c | d 
   | 1 | 1 | 1 |150 
   | 1 | 2 | 3 |100 
   | 1 | 2 | 4 | 50
   | 1 | 1 | 2 |100 

Advertisement

Answer

This sounds like a simple SELECT query:

SELECT a,b,c,SUM(Profit) 
FROM tableName 
GROUP BY a,b,c
ORDER BY a
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement