SQL MS Access 2016 中的服务器 LEAD 等效函数
SQL Server LEAD Equivalent Function in MS Access 2016
我正在尝试在我的 MS ACCESS table 上创建某种类型的有效性(日期范围)字段。需要按照ID、Subject、Level进行分组,得出每条记录的有效期。如果没有参加相应的考试提高成绩,有效日期将设置为31/12/9999。
我现在有以下 table:
ID | Subject | Level | FromGrade | ToGrade | TestDate
101 | Math | 5 | C+ | D | 31/11/2016
101 | Math | 4 | D | A | 01/12/2016
101 | Math | 5 | D | B+ | 12/12/2016
101 | Math | 5 | B+ | A | 25/12/2016
102 | English | 4 | B | B | 20/12/2016
102 | English | 4 | B | C | 28/12/2016
最终结果 table 我应该得到以下结果:
ID | Subject | Level | FromGrade | ToGrade | TestDate | EffectiveTo
101 | Math | 5 | C+ | D | 31/11/2016 | 11/12/2016
101 | Math | 4 | D | A | 01/12/2016 | 31/12/9999
101 | Math | 5 | D | B+ | 12/12/2016 | 24/12/2016
101 | Math | 5 | B+ | A | 25/12/2016 | 31/12/9999
102 | English | 4 | B | B | 20/12/2016 | 27/12/2016
102 | English | 4 | B | C | 28/12/2016 | 31/12/9999
要在 SQL 服务器中执行此操作,我可以轻松使用 LEAD OVER
函数:
SELECT [ID]
, [Subject]
, [Level]
, [FromGrade]
, [ToGrade]
, [TestDate]
, [EffectiveTo] = LEAD([TestDate], 1) OVER ( PARTITION BY [ID], [Subject], [Level] ORDER BY [TestDate] )
into StudentTable2
FROM [dbo].[StudentTable1]
ORDER BY [ID], [Subject], [Level], [TestDate]
然后是
update [dbo].StudentTable2set [EffectiveTo] = DATEADD("DAY", -1, [EffectiveTo]) where EffectiveTo is not null
但是,这不适用于 Access/VBA 女士的脚本,有没有其他方法可以达到相同的效果?
您可以使用相关子查询。要在 MS Access 中获取下一个值:
SELECT st.*,
(SELECT MIN(st2.TestDate)
FROM dbo.StudentTable1 as st2
WHERE st2.ID = st.ID AND
st2.Subject = st.Subject AND
st2.[Level] = st.[Level] AND
st2.TestDate > st.TestDate
) as EffectiveTo
FROM dbo.StudentTable1 as st
ORDER BY [ID], [Subject], [Level], [TestDate];
我正在尝试在我的 MS ACCESS table 上创建某种类型的有效性(日期范围)字段。需要按照ID、Subject、Level进行分组,得出每条记录的有效期。如果没有参加相应的考试提高成绩,有效日期将设置为31/12/9999。
我现在有以下 table:
ID | Subject | Level | FromGrade | ToGrade | TestDate
101 | Math | 5 | C+ | D | 31/11/2016
101 | Math | 4 | D | A | 01/12/2016
101 | Math | 5 | D | B+ | 12/12/2016
101 | Math | 5 | B+ | A | 25/12/2016
102 | English | 4 | B | B | 20/12/2016
102 | English | 4 | B | C | 28/12/2016
最终结果 table 我应该得到以下结果:
ID | Subject | Level | FromGrade | ToGrade | TestDate | EffectiveTo
101 | Math | 5 | C+ | D | 31/11/2016 | 11/12/2016
101 | Math | 4 | D | A | 01/12/2016 | 31/12/9999
101 | Math | 5 | D | B+ | 12/12/2016 | 24/12/2016
101 | Math | 5 | B+ | A | 25/12/2016 | 31/12/9999
102 | English | 4 | B | B | 20/12/2016 | 27/12/2016
102 | English | 4 | B | C | 28/12/2016 | 31/12/9999
要在 SQL 服务器中执行此操作,我可以轻松使用 LEAD OVER
函数:
SELECT [ID]
, [Subject]
, [Level]
, [FromGrade]
, [ToGrade]
, [TestDate]
, [EffectiveTo] = LEAD([TestDate], 1) OVER ( PARTITION BY [ID], [Subject], [Level] ORDER BY [TestDate] )
into StudentTable2
FROM [dbo].[StudentTable1]
ORDER BY [ID], [Subject], [Level], [TestDate]
然后是
update [dbo].StudentTable2set [EffectiveTo] = DATEADD("DAY", -1, [EffectiveTo]) where EffectiveTo is not null
但是,这不适用于 Access/VBA 女士的脚本,有没有其他方法可以达到相同的效果?
您可以使用相关子查询。要在 MS Access 中获取下一个值:
SELECT st.*,
(SELECT MIN(st2.TestDate)
FROM dbo.StudentTable1 as st2
WHERE st2.ID = st.ID AND
st2.Subject = st.Subject AND
st2.[Level] = st.[Level] AND
st2.TestDate > st.TestDate
) as EffectiveTo
FROM dbo.StudentTable1 as st
ORDER BY [ID], [Subject], [Level], [TestDate];