select 同一用户的前一行值(多条记录)
select previous row value for same user (multiple records)
我在 Access 2010 中有一个查询(也曾在 2013 年尝试过,结果相同),该查询对所有记录都有效但并不完美。我想知道是否有人知道导致错误的原因。
这是查询(改编自 http://allenbrowne.com/subquery-01.html#AnotherRecord):
SELECT t_test_table.individ, t_test_table.test_date, t_test_table.score1, (SELECT top 1 Dupe.score1
FROM t_test_table AS Dupe
WHERE Dupe.individ = t_test_table.individ
AND Dupe.test_date < t_test_table.test_date
ORDER BY Dupe.primary DESC, Dupe.individ
) AS PriorValue, [score1]-[priorvalue] AS scorechange
FROM t_test_table;
根据数据的设置方式,一个人在文件中有多个记录(由 individ 指定),代表不同的测试日期。日期和个人的组合是独一无二的——您只能参加一次考试。 [primary] 指的是主键列。我之所以这样做是因为 individ 字段不是主键,因为可以有多个(由于 space,我没有在这里包括它)
以上代码的目标是创建以下内容:
individ test_date score1 PriorValue scorechange
1 3/1/2013 40
1 6/4/2013 51 40 11
1 7/25/2013 55 51 4
1 12/13/2013 59 55 4
5 8/29/2009 39
5 12/9/2009 47 39 8
5 6/1/2010 58 47 11
5 8/28/2010 42 58 -16
5 12/15/2010 51 42 9
这是我实际得到的。您可以看到,对于 individ 1,它最终采用第一个分数而不是每个后续记录的先前分数。对于个人 5,它有点工作,但最终的先验值应该是 42 而不是 58。
individ test_date score1 PriorValue scorechange
1 3/1/2012 40
1 6/4/2012 51 40 11
1 7/25/2012 55 40 15
1 12/13/2012 59 40 19
5 8/29/2005 39
5 12/9/2005 47 39 8
5 6/1/2006 58 47 11
5 8/28/2006 42 58 -16
5 12/15/2006 51 58 -7
有人知道这里出了什么问题吗?在其他记录中,它工作得很好,但我无法确定是什么原因导致某些记录无法采用以前的 value.Any 帮助,如果您需要其他信息,请告诉我。
要获得给定个人的最新测试,您需要包括按日期排序。在您的内部查询中,替换
ORDER BY Dupe.primary DESC, Dupe.individ
和
ORDER BY Dupe.test_date DESC
很难说按 primary
排序有什么影响,因为您还没有告诉我们您是如何生成 primary
的值的。如果保证 individ
和 test_date
的组合是唯一的,您可能希望将 making the two of them 考虑到您的主键中,而不是创建一个新的东西。 ORDER BY
行中的 Dupe.individ
无效,因为您的 WHERE
子句已经将内部查询的结果限制为一个单独的结果。
我在 Access 2010 中有一个查询(也曾在 2013 年尝试过,结果相同),该查询对所有记录都有效但并不完美。我想知道是否有人知道导致错误的原因。
这是查询(改编自 http://allenbrowne.com/subquery-01.html#AnotherRecord):
SELECT t_test_table.individ, t_test_table.test_date, t_test_table.score1, (SELECT top 1 Dupe.score1
FROM t_test_table AS Dupe
WHERE Dupe.individ = t_test_table.individ
AND Dupe.test_date < t_test_table.test_date
ORDER BY Dupe.primary DESC, Dupe.individ
) AS PriorValue, [score1]-[priorvalue] AS scorechange
FROM t_test_table;
根据数据的设置方式,一个人在文件中有多个记录(由 individ 指定),代表不同的测试日期。日期和个人的组合是独一无二的——您只能参加一次考试。 [primary] 指的是主键列。我之所以这样做是因为 individ 字段不是主键,因为可以有多个(由于 space,我没有在这里包括它)
以上代码的目标是创建以下内容:
individ test_date score1 PriorValue scorechange
1 3/1/2013 40
1 6/4/2013 51 40 11
1 7/25/2013 55 51 4
1 12/13/2013 59 55 4
5 8/29/2009 39
5 12/9/2009 47 39 8
5 6/1/2010 58 47 11
5 8/28/2010 42 58 -16
5 12/15/2010 51 42 9
这是我实际得到的。您可以看到,对于 individ 1,它最终采用第一个分数而不是每个后续记录的先前分数。对于个人 5,它有点工作,但最终的先验值应该是 42 而不是 58。
individ test_date score1 PriorValue scorechange
1 3/1/2012 40
1 6/4/2012 51 40 11
1 7/25/2012 55 40 15
1 12/13/2012 59 40 19
5 8/29/2005 39
5 12/9/2005 47 39 8
5 6/1/2006 58 47 11
5 8/28/2006 42 58 -16
5 12/15/2006 51 58 -7
有人知道这里出了什么问题吗?在其他记录中,它工作得很好,但我无法确定是什么原因导致某些记录无法采用以前的 value.Any 帮助,如果您需要其他信息,请告诉我。
要获得给定个人的最新测试,您需要包括按日期排序。在您的内部查询中,替换
ORDER BY Dupe.primary DESC, Dupe.individ
和
ORDER BY Dupe.test_date DESC
很难说按 primary
排序有什么影响,因为您还没有告诉我们您是如何生成 primary
的值的。如果保证 individ
和 test_date
的组合是唯一的,您可能希望将 making the two of them 考虑到您的主键中,而不是创建一个新的东西。 ORDER BY
行中的 Dupe.individ
无效,因为您的 WHERE
子句已经将内部查询的结果限制为一个单独的结果。