Netezza SQL:在窗口框架中指定偏移量

时间:2015-02-05 20:15:30

标签: sql window netezza partition

为窗口分析函数制作“框架”时,可以指定要“回顾”的文字行数。例如,以下将获得一个家庭每周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

嗯......很高兴我问道。但无论如何,你知道是否有一个功能可以让我在分区规范本身中指定偏移量吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

尝试在窗口范围规范中使用between

sum(sales) over (partition by household_id
                 order by week_id
                 rows between 34 preceding and 8 preceding
                ) as x34