MySQL Forums
Forum List  »  Newbie

Carry out SUM and place result into a different table
Posted by: Ron O Coig
Date: May 02, 2020 04:53AM

What I am trying to accomplish for two days now:
In Table 1 (table_original)
I need to take the last entry on the recent row, and take each column and process a SUM for each column.
Once this sum is carried out, the result for each column is then placed into a different table (table_result) under the same column header with avg added to the end of the name.
I am able to take the columns and complete the sum on the last entry into the database,
Once I have the result I am unable to then send that to the other table to be stored, all within the same script.
The result should over write the existing sum in the column in second table (result).
Here is what I have tried.

SELECT
columnone,
SUM(column_1 / 147707 * 100000) columnoneavg,
columntwo,
SUM(columntwo / 56932* 100000) columntwoavg,
columnthree,
SUM(columnthree / 542868 * 100000) columnthreeavg
FROM
county_per_capita
GROUP BY columnone
ORDER BY columnoneavg DESC
INSERT INTO county_per_capita_two(columnoneavg, columntwoavg, columnteavg);

The above will give me the correct result,

Now my issue is how can I then automatically put the result for each column into the second table to be stored, also the result should override the existing data for that column.

I do get error INSERT is not valid in this position, but can not work out how to do this, and I can not find tutorials on how this can be done.

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.