取得上个月的记录

姆拉杜尔·古普塔
item    loc     year    month   quantity        startdate
XYZ     A       2020    1       3               23-06-2020
ABC     B       2020    2       218             24-06-2020
SDC     C       2020    6       107             25-06-2020
QWE     D       2020    7       144             25-06-2020
XYZ     A       2019    12      89              23-06-2020
ABC     B       2019    11      218             24-06-2020
SDC     C       2020    5       117             25-06-2020
QWE     D       2020    6       144             25-06-2020

如果我考虑上表,那么我的输出应如下所示:

item    loc     year    month   quantity        startdate
XYZ     A       2020    1       89              23-06-2020
ABC     B       2020    2       3               24-06-2020
SDC     C       2020    6       117             25-06-2020
QWE     D      2020    7        144             25-06-2020

因此,您可以看到只有数量值发生了变化,而我们从前几个月中获取的值和其余列中的值保持不变。

专线小巴

看起来您想要窗口功能lag()对于您的样本数据,这将产生所需的结果:

select *
from (
    select 
        item, 
        loc,
        year,
        month,
        lag(quantity) over(partition by item, loc order by year, month) quantity,
        startdate
    from mytable
) t
where quantity is not null

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章