Postgresql使用累积和更新列
你能帮我做一个postgresql查询吗?我有一张“abc公司”的桌子。它有两个主键(id,year),第三列是数字值,第四列应填写每年第三列值的累积和。我尝试了以下方法:Postgresql使用累积和更新列,sql,postgresql,sum,sql-update,inner-join,Sql,Postgresql,Sum,Sql Update,Inner Join,你能帮我做一个postgresql查询吗?我有一张“abc公司”的桌子。它有两个主键(id,year),第三列是数字值,第四列应填写每年第三列值的累积和。我尝试了以下方法: update company_abc set srti = ww.srti from (SELECT sum(company_abc.salestotal) over (partition by year order by salestotal desc) as stri) ww; 并尝试: update com
update company_abc
set srti = ww.srti
from (SELECT sum(company_abc.salestotal) over (partition by year order by salestotal desc) as stri) ww;
并尝试:
update company_abc
set (cid, year, srti) = (SELECT company_abc.cid, company_abc.year, sum(company_abc.salestotal) over (partition by year order by salestotal DESC)
FROM company_abc
ORDER BY year desc);
在这两种情况下,它都失败了。救命啊 您似乎想要一个带有自我加入的
更新:
update company_abc c
set stri = c1.stri
from (
select id, year,
sum(salestotal) over(partition by year order by salestotal desc) stri
from company_abc
) c1
where c1.id = c.year and c1.id = c.id
子查询计算窗口和(使用与原始代码中相同的表达式),然后我们使用表的主键更新表中相应行的值。非常感谢!我花了半天的时间在这上面,你在几秒钟内就完成了!))