Home > OS >  How to make SUM by ID and INSERT INTO
How to make SUM by ID and INSERT INTO

Time:06-24

I have the next question, and table : table.op1

This being my table, how to merge all the data with summing up the data only from baza and tva.

The output can be in a different table, cause I will delete all the info in this table, and INSERT it back into it, from the new table.

CuiP is the unique identifier of the company, that should be used to SUM all the values from baza and tva. In a mention, the SUM should be made for every company apart and to not be summed with other companies.

Eg: Society 1 - tva 500 - baza 100
Society 1 - tva 1000 - baza 500
Society 2 - tva 100 - baza 100
Society 2 - tva 200 - baza 150
Should be: Society 1 - tva 1500 - baza 600
: Society 2 - tva 300 - baza 250

Note that the table has over 1500 entries, I cannot make a sum for every company apart.

Thanks!

CodePudding user response:

Your table should hold the company information and the desired totals of the mentioned columns. Then use sum() function to aggregate your columns by company.

create table1 (denP varchar(100), sum_baza(int), sum_tva(int))

insert into table1(
select denP, sum(baza), sum(tva) from table.op1
group by denP)
  • Related