我正在尝试将滞后函数转换为在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
要遵循链接的解决方案,请考虑为
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]