I have written a computationally expensive select query which produces a certain result set. I need to insert this result into a table and the aggregates of this result set into another table. I need to do those in a single transaction instead of using two transactions as it can lead to inconsequential data issues.
Example scenario I have a table having data regarding employee name, department and salary
| name | department | salary |
|---|---|---|
| Jason | Finance | 100 |
| Bourne | Finance | 120 |
| Michael | Finance | 90 |
| Robert | Sales | 75 |
| Downey | Sales | 500 |
| Matthew | Engineering | 10 |
| Tom | Engineering | 90 |
I want to insert data from this table into table A with sum of salary per department greater than 100. Into table B I want to insert a row for each department with sum of salary per department. Hence my table A would be like
| name | department | salary |
|---|---|---|
| Jason | Finance | 100 |
| Bourne | Finance | 120 |
| Michael | Finance | 90 |
| Robert | Sales | 75 |
| Downey | Sales | 500 |
and table b would be like
| department | salary |
|---|---|
| Finance | 310 |
| Sales | 575 |
| Engineering | 100 |
How can I do this in Oracle?