Answer the question
In order to leave comments, you need to log in
How to add an amount from another table to an empty column of a table according to a given condition?
Suppose I have the following table:
column_1 | column_2 | column_3
__________ |___________ |__________
1 | 12 |
1 | 32 |
2 | 10 |
2 | 5 |
3 | 6 |
3 | 10 |
Please tell me what function should be written so that the sum from column_2 is calculated in column_3, taking into account the grouping with column_1? The final table should look like this:
column_1 | column_2 | column_3
__________ |___________ |__________
1 | 12 | 44
1 | 32 | 44
2 | 10 | 15
2 | 5 | 15
3 | 6 | 16
3 | 10 | sixteen
Answer the question
In order to leave comments, you need to log in
select
M.имя_маршрута
,G.имя_города
,G2.имя_города
from Маршруты M
inner join Города G ON M.id_пункта_отправления=G.ID
inner join Города G2 ON M.id_пункта_назначения=G2.ID
Automatically update the table - it needs a trigger.
You can make a view (VIEW) and then, when requesting data from this view, the amount you need will be automatically calculated each time.
create table tbl (
сol1 int,
col2 int
);
insert into tbl (col1, col2) values (1, 12), (1, 32), (2, 10), (2, 5), (3, 6), (3, 10);
CREATE VIEW tbl_sum AS SELECT tbl.*, SUM(tbl.col2) OVER (PARTITION BY tbl.col1) AS col3 FROM tbl;
SELECT * FROM tbl_sum;
update tbl
join (
select col1, sum(col2) col2_sum from tbl group by col1
) summed on summed.col1 = tbl.col1
set col3 = col2_sum;
Can you please tell me what function should be written so that the sum from column_2 is calculated in column_3, taking into account the grouping with column_1?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question