基于 Table 中出现次数的后缀字段值

Suffix Field Values based on number of occurrences in Table

我在 MS Access 中有一个 table,它有一个字段 layout_desc。我需要创建一个查询,通过添加值在 table.

中重复的次数来更改此字段中的每个值

例如:

谢谢。

没有主键:

假设您的 table 包含一个主键字段,通过该字段使用域聚合函数对记录进行排序,一种可能的方法是在 VBA.

  • 使用Alt+F11
  • 打开VBAIDE
  • 插入一个新的Public模块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 更改为您的 table 的名称:

    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)
    

有一个主键:

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

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