sql - How can i sum rows when a certain column value are equal from another table and insert them to a third table? -
sql - How can i sum rows when a certain column value are equal from another table and insert them to a third table? -
i need sum rows when column equal re-create them table when column = column.
better write downwards tables u guys can understand it.
table1 +------+------+------+ | id | col1 | col2 | +------+------+------+ | 1658 | 86 | 117 | | 1665 | 65 | 14 | | 1667 | 66 | 72 | | 1669 | 78 | 12 | +------+------+------+ table2 +--------+------+------+ | areaid | id | col1 | +--------+------+------+ | 1 | 1658 | bla | | 1 | 1665 | bla | | 2 | 1667 | bla | | 2 | 1669 | bla | +--------+------+------+ table3 +--------+------+------+ | areaid | col1 | col2 | +--------+------+------+ | 1 | 0 | 0 | | 2 | 0 | 0 | +--------+------+------+well want calculating sum of col1 , col2 table1 when id.table1 = id.table2 , areaid.table2 equal !, update table3 sum of rows. seems kinda hard it's of import me.
any help appreciated, help.
you need join
inner select
in update
statement while aggregating columns sum
. if understood column construction , question correctly, should need:
update t3 set col1 = u.col1total, col2 = u.col2total table3 t3 bring together ( select t2.areaid, sum(t1.col1) col1total, sum(t1.col2) col2total table1 t1 bring together table2 t2 on t1.id = t2.id grouping t2.areaid ) u on u.areaid = t3.areaid
sql sql-server
Comments
Post a Comment