2
SELECT sku, sum(quantity)
FROM orders
WHERE "location" = 'Location - 1'
group by sku;

This returns 2 columns:

Col1 = Sku
Col2 = sums of quantity for each sku

I have another table that contains skus in Col1. I then want to insert into table2 the values from Col2 where Col1 = table2.col1 (matches on orders.sku and table2.sku, inserts sum value for orders.sku)

2
  • You speak of "insert", but it sounds like you want to update? Commented Jan 25, 2019 at 3:08
  • Any feedback here? Commented Feb 4, 2019 at 16:59

2 Answers 2

0

I think you wanted a select insert like this one:

insert into table2(sku, result)
select sku, sum(quantity) as result
from orders
group by sku;
Sign up to request clarification or add additional context in comments.

Comments

0

Sounds like you really want to UPDATE:

UPDATE table2 t
SET    col2 = o.sum_q
FROM  (
   SELECT sku, sum(quantity) AS sum_q
   FROM   orders
   WHERE  location = 'Location - 1'
   GROUP  BY sku
   ) o
WHERE  t.sku = o.sku
AND    t.col2 IS DISTINCT FROM o.sumq;

The last line is optional, but typically a good idea to avoid empty updates. See:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.