Reputation: 4635
I am retrieving the SUM
of some data from a query. The SUM
can have both negative and positive values. I want to insert the positive values to one table and the negative values to another table.
The result set for the select query is like below
total | userid
-----------------------
4750.00 | 11
1339.00 | 3607
-681.81 | 3600
I was planning to insert the details from the select query directly to the table, INSERT into table (amount,user) SELECT SUM(..) AS total,userid FROM...
. But I was not able to figure out how to do it in a single query.
I am using PostgreSQL 8.4.17
Upvotes: 0
Views: 625
Reputation: 1271151
Use the where
statement in two separate inserts
:
INSERT into positives (amount,user)
SELECT SUM(..) AS total, userid
FROM...
having sum(..) > 0;
INSERT into negatives (amount,user)
SELECT SUM(..) AS total, userid
FROM...
having sum(..) < 0;
Upvotes: 2