滞后函数转换为 MS Access SQL

问题描述 投票:0回答:1

我正在尝试将滞后函数转换为在access-sql中使用,它根本没有该函数。所以我有一个表 A,其中包含列类型(a、b 或 c)、决策者(1 或 2)、日期和值..我想创建另一列,它只需将值向下移动一个日期,然后使用第一个值为空。

我尝试使用下面的这个线程,但我想我仍然做错了什么:

计算一行到下一行的列值差异

我的尝试是

select type, dates, values, 
   (select top 1 test.values 
    from test as m
    where m.dates < test.dates 
    group by test.type, test.values, test.dates
    order by test.values,test.dates desc
   ) as lag
from test
group by test.type, test.dates,test.values
order by test.dates
sql ms-access lag
1个回答
0
投票

要遵循链接的解决方案,请考虑为

WHERE
列添加
type
条件,而不需要
GROUP BY
:

select [type], [dates], [values], 
   (select top 1 test.[values]
    from test as m
    where m.[dates] < test.[dates]
      and m.[type] = test.[type]
    order by test.[dates] desc
   ) as lag
from test
order by test.[type], test.[dates]
© www.soinside.com 2019 - 2024. All rights reserved.