I see there is an error in the last line. Can someone please tell me what is the alternative?
INSERT INTO month_week_qty
(
site_id, cat_id, cat_name, scat_id, scat_name, sscat_id, sscat_name, duration, w_m, year, sum_qty
)
SELECT
c.site_id, NULL, NULL, NULL, NULL, NULL, NULL, 'month', MONTH(s.created_at), YEAR(s.created_at), ROUND( SUM( s.qty_ordered ) ) as qty
FROM cat_products c, sku_qty_brand s
WHERE (c.product_id = s.product_id)
GROUP BY c.site_id, MONTH(s.created_at)
ON DUPLICATE KEY UPDATE sum_qty = sum_qty + ROUND( SUM( s.qty_ordered ) )
You cannot use aggregates in the
ON DUPLICATE KEY UPDATEclause, but you can put your grouped aggregates in a subquery instead:(I omitted the NULL columns for clarity.)