【问题标题】:Suffix Field Values based on number of occurrences in Table基于表中出现次数的后缀字段值
【发布时间】:2019-08-01 20:45:50
【问题描述】:

我在 MS Access 中有一个表,其中有一个字段 layout_desc。我需要创建一个查询,通过添加该值在表中重复的次数来更改该字段中的每个值。

例如:

谢谢。

【问题讨论】:

  • 你的表有主键字段吗?

标签: sql ms-access vba ms-access-2016


【解决方案1】:

没有主键:

假设您的表包含使用域聚合函数对记录进行排序的主键字段,一种可能的方法是在 VBA 中使用静态变量。

  • 使用 Alt+F11 打开 VBA IDE
  • 插入一个新的公共模块 Alt+I,M
  • 将以下基本代码复制到新模块中:

    Function Occurrence(Optional strVal As String) As Long
        Static lngTmp As Long
        Static strTmp As String
        If strTmp = strVal Then
            lngTmp = lngTmp + 1
        Else
            lngTmp = 1
            strTmp = strVal
        End If
        Occurrence = lngTmp
    End Function
    
  • 在 MS Access 中,使用以下 SQL 创建一个新查询,将 YourTable 更改为您的表名:

    update (select t.layout_desc from YourTable as t order by t.layout_desc) q
    set q.layout_desc = q.layout_desc & occurrence(q.layout_desc)
    

使用主键:

如果您的表要包含长整数数据类型的主键,例如 id,您可以按以下方式使用域聚合函数 DCount

update YourTable t
set t.layout_desc = t.layout_desc & 
dcount("*","YourTable","layout_desc = '" & t.layout_desc & "' and id <= " & t.id)

【讨论】:

  • @ali 不客气!如果我的回答充分回答了您的问题,请将答案标记为解决方案(如果您认为合适,请投票),以便其他浏览该网站的人看到该问题已解决。如果您不确定如何执行此操作,请参阅this article。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-11
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多