PostgreSQL - 使用另一个 table 的先前值更新 table
PostgreSQL - Update table with previous values from another table
我想更新 table 一秒的总和 table
这是我要更新的 table 'x'。有起始值和结束值:
id
op_date
initial_value
end_value
1
2020-02-01
0
0
1
2020-02-02
0
0
2
2020-02-01
0
0
2
2020-02-02
0
0
table'y'保存当天的数值:
id
op_date
value_day
1
2020-01-29
500
1
2020-02-01
100
1
2020-02-02
200
2
2020-01-29
750
2
2020-02-01
100
2
2020-02-02
250
我希望结果如下所示:
id
op_date
initial_value
end_value
1
2020-02-01
500
600
1
2020-02-02
600
800
2
2020-02-01
750
850
2
2020-02-02
850
1100
我试过这个脚本,但是进程只是运行它而没有完成它:
UPDATE x
SET
initial_value= (select sum(y.value_day)
from public.y where
y.op_date > '2020-11-01' and y.op_date < x.op_date
and y.id = x.id),
end_value= (select sum(y.value_day)
from public.y where
y.op_date between '2020-11-01' and x.op_date
and y.id = x.id);
您可以使用window函数。要进一步了解 window 功能,您可以查看此 link。
起初我正在写查询 select 值。
select id,op_date,
sum(value_day) over (
partition by y.id
rows between unbounded preceding and current row
)-value_day as initial_value,
sum(value_day) over (
partition by y.id
rows between unbounded preceding and current row
) as end_value
from y
;
这是您的更新查询。
UPDATE x
set initial_value=s_statement.initial_value,
end_value=s_statement.end_value
from
(select id,op_date,
sum(value_day) over (
partition by y.id
rows between unbounded preceding and current row
)-value_day as initial_value,
sum(value_day) over (
partition by y.id
rows between unbounded preceding and current row
) as end_value
from y) s_statement
where x.id=s_statement.id
and x.op_date=s_statement.op_date
;
让我知道你是否满意。
我想更新 table 一秒的总和 table
这是我要更新的 table 'x'。有起始值和结束值:
id | op_date | initial_value | end_value |
---|---|---|---|
1 | 2020-02-01 | 0 | 0 |
1 | 2020-02-02 | 0 | 0 |
2 | 2020-02-01 | 0 | 0 |
2 | 2020-02-02 | 0 | 0 |
table'y'保存当天的数值:
id | op_date | value_day |
---|---|---|
1 | 2020-01-29 | 500 |
1 | 2020-02-01 | 100 |
1 | 2020-02-02 | 200 |
2 | 2020-01-29 | 750 |
2 | 2020-02-01 | 100 |
2 | 2020-02-02 | 250 |
我希望结果如下所示:
id | op_date | initial_value | end_value |
---|---|---|---|
1 | 2020-02-01 | 500 | 600 |
1 | 2020-02-02 | 600 | 800 |
2 | 2020-02-01 | 750 | 850 |
2 | 2020-02-02 | 850 | 1100 |
我试过这个脚本,但是进程只是运行它而没有完成它:
UPDATE x
SET
initial_value= (select sum(y.value_day)
from public.y where
y.op_date > '2020-11-01' and y.op_date < x.op_date
and y.id = x.id),
end_value= (select sum(y.value_day)
from public.y where
y.op_date between '2020-11-01' and x.op_date
and y.id = x.id);
您可以使用window函数。要进一步了解 window 功能,您可以查看此 link。 起初我正在写查询 select 值。
select id,op_date,
sum(value_day) over (
partition by y.id
rows between unbounded preceding and current row
)-value_day as initial_value,
sum(value_day) over (
partition by y.id
rows between unbounded preceding and current row
) as end_value
from y
;
这是您的更新查询。
UPDATE x
set initial_value=s_statement.initial_value,
end_value=s_statement.end_value
from
(select id,op_date,
sum(value_day) over (
partition by y.id
rows between unbounded preceding and current row
)-value_day as initial_value,
sum(value_day) over (
partition by y.id
rows between unbounded preceding and current row
) as end_value
from y) s_statement
where x.id=s_statement.id
and x.op_date=s_statement.op_date
;
让我知道你是否满意。