1
votes

Please I need help in adding the values of 3 rows in my table and adding the total sum into another row. For example:

I want the sum of values from row1 to row3 be added to row4. I have done a lot and done know how to continue any further

row1|row2|row3|row4

100 |350 |500 |

350 |298 |123 |

999 |234 |277 |

Your kind help is appreciated.

Mike

3
Rows or columns? You've called them rows but they look like columns to me.paxdiablo

3 Answers

1
votes

First, if they are actually rows in the database, you can simply create a new row containing the sums:

insert into mytable (columnA, columnB, columnC)
    select sum(columnA), sum(columnB), sum(columnC) from mytable

But it's rather unusual to do that in the actual table unless you have a way of distinguishing these "totals" rows from the regular ones.


If they're columns, as appears from the layout, there are some issues.

First, what you're attempting will violate third normal form, which is rarely a good thing. You need to think of what will happen if someone changes one of the columns but doesn't also update the totals column.

But, if you want to do this, it's a simmple matter of:

update mytable set columnD = columnA + columnB + columnC
0
votes
SELECT row1, row2, row3, row1 + row2 + row3 AS row4 FROM wherever;

and really - try to solve it on your own and using google first.

0
votes

SELECT row1, row2, row3, SUM(row1 + row2 + row3) AS row4 FROM table_name;