Skip to content

Instantly share code, notes, and snippets.

@helloris25
Last active July 21, 2017 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helloris25/ec73e8fefa28de3594899ffc3af65b91 to your computer and use it in GitHub Desktop.
Save helloris25/ec73e8fefa28de3594899ffc3af65b91 to your computer and use it in GitHub Desktop.
Обновление полей в которых суммы из другой таблицы с условием
UPDATE Table_1 T_1
LEFT JOIN
(
SELECT
id_table1,
Sum(CASE
WHEN direction = 'one'
THEN volume
ELSE 0
END) AS sum_one,
Sum(CASE
WHEN direction = 'two'
THEN volume
ELSE 0
END) AS sum_two,
Sum(CASE
WHEN direction = 'three'
THEN volume
ELSE 0
END) AS sum_three
FROM Table_2
GROUP BY id_table1
) T_2
ON T_1.id = T_2.id_table1
SET
T_1.total_one = T_2.sum_one,
T_1.total_two = T_2.sum_two,
T_1.total_three = T_2.sum_three;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment