0

如果有人让我知道如何在 MSAccess 中实现“PreviousValue” cloumn,则不胜感激

我将记录存储在“总体”以外的各种类别的表中,并且基于 ID/名称/办公室/产品/类别组合我想通过 MS Access 功能/高效查询来实现以前的值列

ID       Name        Office Product Overall    Month         Value  Previous
228      Woodlands  london  Europe  Sales      201106          6    0
228      Woodlands  london  Europe  Sales      201202          3    6
228      Woodlands  london  Europe  Sales      201207          4    3
228      Woodlands  london  Europe  Overall    201106          6    0
228      Woodlands  london  Europe  Overall    201202          3    6
228      Woodlands  london  Europe  Overall    201207          4    3
228      Woodlands  london  Europe  Exetion    201202          6    0
228      Woodlands  london  UK      Exetion    201202          6    1
228      Woodlands  london  UK      Exetion    201106          1    0
228      Woodlands  london  Europe  Exetion    201207          6    6
228      Woodlands  london  Europe  Content    201106          6    0
228      Woodlands  london  UK      Content    201106          1    0
228      Woodlands  london  Europe  Content    201202          3    6
228      Woodlands  london  UK      Content    201202          1    1
228      Woodlands  london  UK      Content    201207          1    1
228      Woodlands  london  Europe  Content    201207          4    3

提前致谢 !!


查询结果--我猜是因为 Top1 函数而不是预期的结果,正确的输出如下图所示

 ID  Name      Office   Product Category    Date       Month    ValuePrevious
228  Investors  london  Europe  Content   01/06/2011    201106  6   0
228  Investors  london  Europe  Content   23/02/2012    201202  3   6
228 Investors   london  Europe  Content   01/07/2012    201207  4   6

在此处输入图像描述

4

1 回答 1

0

您可以使用子查询来获取先前的值。

SELECT tx.id,
       tx.name,
       tx.office,
       tx.product,
       tx.overall,
       tx.month,
       tx.VALUE,
       tx.previous,
       Nz((SELECT TOP 1 [value]
           FROM   tablename t
           WHERE  t.id = tx.id
                  AND t.name = tx.name
                  AND t.office = tx.office
                  AND t.product = tx.product
                  AND t.overall = tx.overall
                  AND t.month < tx.month
           ORDER  BY t.month, rnd(t.id) DESC), 0) AS Prev
FROM   tablename tx
ORDER  BY tx.month; 

基于更改的、非常稀疏的样本数据的新查询

SELECT tz.id, 
       tz.name, 
       tz.office, 
       tz.product, 
       tz.date, 
       tz.month, 
       tz.VALUE, 
       Nz((SELECT TOP 1 [value] 
           FROM   tablename t 
           WHERE  t.id = tz.id 
                  AND t.name = tz.name 
                  AND t.office = tz.office 
                  AND t.product = tz.product 
                  AND t.date < tz.date
           ORDER  BY t.date DESC), 0) AS Prev 
FROM   tablename tz 
ORDER  BY tz.date; 
于 2012-08-17T09:00:13.840 回答