I
I
Ivan_R22020-09-14 15:42:57
PostgreSQL
Ivan_R2, 2020-09-14 15:42:57

How to assign the results of a SQL query to another column?

Good afternoon!
I have a test_field column and have a query that outputs the results as the
5f5f649adae56930429881.png

query itself:

SELECT my_table.id AS id, sum(child.qty) AS all_child_qty
FROM my_table
LEFT OUTER JOIN my_table as child
   ON child.my_path LIKE (my_table.my_path || '%')
GROUP BY my_table.id

Tell me, please, how can I write the values ​​of all_child_qty to the corresponding rows of the test_field column and do it in the same query as the calculation?
Everything is in one table my_table.
Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Melkij, 2020-09-14
@Ivan_R2

Rewrite to correlated query:

update my_table set all_child_qty = (select sum(child.qty) from my_table child where child.my_path LIKE (my_table.my_path || '%'));

or update my_table set ... from (subquery) where ...

S
Stalker_RED, 2020-09-14
@Stalker_RED

UPDATE my_table
LEFT OUTER JOIN my_table as child
   ON child.my_path LIKE (my_table.my_path || '%')
SET all_child_qty = sum(child.qty)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question