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)

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
slinkfresh
  • 63
  • 1
  • 4

2 Answers2

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;
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
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:

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228