How do I add the values of a column and then insert it in different table column? e.g. I have a layer1 with attributes as follow: enter image description here

and layer2 with attributes:

enter image description here

I am trying to sum the MIN with the same ids at layer1 then insert the result in layer2.


There is no GIS-specific aspect to your question, however, if you are using a database like PostGIS with your QGIS you can do the following: (I haven't actually checked if the syntax is correct):

WITH temp AS ( 
SELECT  the_branch, SUM(min)  as min_sum 
FROM layer1 
GROUP BY the_branch) 
UPDATE layer2
SET layer2.total_min=temp.min_sum
FROM temp
WHERE layer1.branch=temp.the_branch;
  • you could also use a virtual layer and a query (have not checked it either) like this. – MrXsquared Jan 7 '19 at 11:17
  • @MrXsquared, good idea, haven't used it myself before though ! – muri Jan 7 '19 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.