为窗口分析函数制作“框架”时,可以指定要“回顾”的文字行数。例如,以下将获得一个家庭每周26周的销售额。
,sum(sales) over (partition by household_id order by week_id rows 26 preceding) as x26
但是......如果你想用偏移量回顾(或向前)怎么办?例如,如果是第n周,您想要在第n周前8周结束的26周的销售额?在我打字的时候,我突然意识到我可能会部分地这样做。即,
,sum(sales) over (partition by household_id order by week_id rows 34 preceding) as x34
,sum(sales) over (partition by household_id order by week_id rows 8 preceding) as x8
...并trailing26_offeset8 = x34 - x8
谢谢!
答案 0 :(得分:2)
尝试在窗口范围规范中使用between
:
sum(sales) over (partition by household_id
order by week_id
rows between 34 preceding and 8 preceding
) as x34