mardi 27 janvier 2015

Inserting multiple formulas into columns sql

I have a question about inserting multiple formulas into a data table, using sql.


I have a large data table, table g. Some columns of g are



id1 id2 xyz
******************************
123 456 4
123 123 1
789 456 5
456 123 2
123 789 4


I have created another table, placeholder, using these commands



CREATE TABLE placeholder(id, count, sum_1, sum_2)


Next, I am trying to insert data into placeholder and am running into an issue.


The variable 'id' in 'placeholder' can match one can match neither, one, or two of id1 id2. I have the count variable coded correctly, but I am struggling with a formula to insert data into sum_1 and sum_2.


sum_1 needs to be a sum of xyz, grouped by id1


sum_2 needs to be a sum of xyz, grouped by id2


I am trying to insert the data into placeholder using this code



INSERT INTO placeholder
SELECT
id1,
COUNT(*) AS 'num' FROM g GROUP BY id1,
SUM(xyz) AS 'num' FROM g GROUP BY id1,
SUM(xyz) AS 'num' FROM g GROUP BY id2


The first two lines work but the last two lines (beginnign with SUM) are not working. I keep getting the error OperationalError: near "SELECT": syntax error .


How do I calculate the conditional sums and insert them into my table?


Aucun commentaire:

Enregistrer un commentaire