【问题标题】:How to take a cumulative previous year record?如何取得上一年的累计记录?
【发布时间】:2014-01-20 16:16:05
【问题描述】:

在 MS Access 中,我有一个如下表:

FY    Percent   
2015    5%  
2016    5%  
2017    5%  
2018    5%  
2019    5%  
2020    5%  

现在我想添加一个计算行,该行的计算应该如下所示:

FY      Calculated
2015    P * 1 (Multiply the value by 1 for first year)
2016    P * 2015 Calculated value (the above value)
2017    P * 2016 Calculated Value
2018    P * 2017 Calculated Value
2019    P * 2018 Calculated Value
2020    P * 2019 Calculated Value
2021    P * 2020 Calculated Value

如何查询?

【问题讨论】:

  • 据我所知,在纯 Access SQL 中没有办法做到这一点……你能(你愿意)为此使用 VBA 过程吗?
  • 没有 Barranka,我知道在 VBA 或 Excel 公式中很容易做到
  • 嗨@Barranka,如果用普通查询根本不可能......我必须使用MS Access VBA。你能帮我吗?

标签: sql ms-access


【解决方案1】:

在普通的 Access 的 SQL 方言中无法做到这一点。因此,VBA 是不二之选。

您需要创建一个表格来保存这些值。假设您的输出表是这样的:

表:tbl_output

FY            Integer
Calculated    Double

VBA 代码:

public sub fillCalculatedValues()
    Dim db as DAO.Database, rsIn as DAO.RecordSet, rsOut as DAO.RecordSet
    Dim strSQL as String
    Dim value as Double, fy as Integer, i as Integer

    ' Connect to the current database
    Set db = currentDb()

    ' Get the values from your input table (let's say it is called tbl_in) in read only mode
    strSQL = "SELECT * FROM tbl_in ORDER BY FY"
    rsIn = db.OpenRecordSet(strSQL, dbOpenDynaset, dbReadOnly)

    ' Initialize your output table
    strSQL = "DELETE FROM tbl_output"
    DoCmd.RunSQL strSQL

    ' Open the output table (allowing edits)
    rsOut = db.OpenRecordset("tbl_output", dbOpenDynaset, dbEditAdd)

    ' Read the input row by row, and insert a new row in the output
    with rsIn
        .moveFirst
        i = 1
        value = 1
        do
            fy = ![FY]     ' Read the field FY from rsIn
            ' Insert the new row in the output table
            rsOut.addNew
                rsOut![FY] = fy
                rsOut![calculated] = value
            rsOut.update
            ' Update the calculated value
            value = value * ![percent]
            ' Advance one row
            .moveNext
        loop until .EOF  ' Loop until you get to the end of the table
    end with
    ' Close everything
    rsOut.close
    rsIn.close
    db.close
end sub

这只是一个例子。您应该修改它以满足您的特定需求

希望这会有所帮助。


PS:

  • 给你一个小彩蛋:The Ten Commandments of Access
  • 如果您愿意从 Access 升级到更强大的 RDBMS,我建议您尝试使用 MySQL。您可以在 MySQL 中使用一些技巧来执行此类操作。您可以take a look to this post 了解如何对 MySQL 中的一组行进行累积求和,并满足您的需求(毕竟,您正在制作累积产品)

【讨论】:

    【解决方案2】:

    我不确定你的意思,也许

    SELECT t.FY, 
         t.Calculated, 
         Nz((SELECT Max(CPercent) 
             FROM tFY 
             WHERE FY< t.FY),1) * Percent AS [Last Value]
    FROM tFY AS t
    

    然后,重新评论

    SELECT t.FY,
          (SELECT Sum(Percent) FROM tFY WHERE FY<= t.FY) AS [Calc Value]
    FROM tFY AS t;
    

    【讨论】:

    • Thaks Remou,不是在寻找这个.. 实际上 max(Percent) 是计算出的 max(value)。我已经编辑了我的问题以使其更容易..请检查现在是否清楚
    【解决方案3】:

    试试这个可能对你有帮助

    select fy,Percent as curr_percent, sum(Percent) over (order by fy) as cal_field from tbl_cal_field;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      • 2020-09-15
      • 1970-01-01
      相关资源
      最近更新 更多