Home > Net >  How to sum values from different rows based on same id?
How to sum values from different rows based on same id?

Time:11-26

Let's assume I have this table:

|    ID    |    Amount   | Value |
|----------|-------------|-------|
| 1        | 3           | 10    |
| 1        | 1           | 25    |
| 2        | 1           | 35    |

How can I transform into this?:

|      ID       | Total Value | 
|---------------|-------------|
| 1             |     55      |
| 2             |     35      | 

Edit: showing how to sum the values without doing the multiplication on the amount would also be useful for my further research.

CodePudding user response:

try this:

Select id,Sum(Amount   *Value) as TotalValue
from table 
group by id

CodePudding user response:

You can multiply the amount by value and use teh aggreagation function SUM on the multiplication

SELECT
   ID,
   SUM( amount * value) as total_value
FROmtable1
GROUPO BY ID  
  •  Tags:  
  • sql
  • Related