Postgresql 不带自联接的前第n行
我有一个大型查询,结果如下:Postgresql 不带自联接的前第n行,postgresql,window-functions,Postgresql,Window Functions,我有一个大型查询,结果如下: Date Cost ------------------ 6-Oct 24.05 5-Oct 34.56 4-Oct 24.76 3-Oct 12.89 2-Oct 11.34 1-Oct 21.76 30-Sept 32.89 29-Sept 11.34 我正在尝试使用windows进行以下操作 Date Cost Previous_week ----------------------------
Date Cost
------------------
6-Oct 24.05
5-Oct 34.56
4-Oct 24.76
3-Oct 12.89
2-Oct 11.34
1-Oct 21.76
30-Sept 32.89
29-Sept 11.34
我正在尝试使用windows进行以下操作
Date Cost Previous_week
-------------------------------
6-Oct 24.05 32.89
5-Oct 34.56 11.34
4-Oct 24.76 NULL
3-Oct 12.89 NULL
2-Oct 11.34 NULL
1-Oct 21.76 NULL
30-Sept 32.89 NULL
29-Sept 11.34 NULL
似乎窗口函数只能用于聚合,是否有其他方法可以获取前一行的第7行
我不想做自我连接。我应该多花点时间阅读文档
select date, cost
nth_value(cost, 7)
over(order by date desc
rows between unbounded preceding and unbounded following)
as previous_week
array\u agg
带窗口,然后选择第一个。听起来很奇怪延迟(成本,7)超过(按日期订购)
。顺便说一句:date
是一个可怕的列名称。它是一个计算表,只是一个例子。感谢tipI。我不知道“计算表”是什么意思,如果您使用CTE或将select封装到派生表中,您可以将其应用于任何结果。原始postgres手册:。红移并不是真正的postgres。