MS Access-发生顺序

时间:2018-02-02 14:41:46

标签: ms-access count

我正在努力解决MS Access中的以下问题:我想为每个记录分配其发生的第n个顺序(第1次,第2次非总计出现次数)。

与Excel相比,它应该像移动范围的countif一样工作。例如,如果值存储在列B中,那将是

=countif($B$1:B1,B1)

2 个答案:

答案 0 :(得分:0)

您可以使用我的 RowCounter 功能。

请研究典型用法的在线评论:

Public Function RowCounter( _
  ByVal strKey As String, _
  ByVal booReset As Boolean, _
  Optional ByVal strGroupKey As String) _
  As Long

' Builds consecutive RowIDs in select, append or create query
' with the possibility of automatic reset.
' Optionally a grouping key can be passed to reset the row count
' for every group key.
'
' Usage (typical select query):
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable
'   WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True));
'
' Usage (with group key):
'   SELECT RowCounter(CStr([ID]),False,CStr[GroupID])) AS RowID, *
'   FROM tblSomeTable
'   WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True));
'
' The Where statement resets the counter when the query is run
' and is needed for browsing a select query.
'
' Usage (typical append query, manual reset):
' 1. Reset counter manually:
'   Call RowCounter(vbNullString, False)
' 2. Run query:
'   INSERT INTO tblTemp ( RowID )
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable;
'
' Usage (typical append query, automatic reset):
'   INSERT INTO tblTemp ( RowID )
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable
'   WHERE (RowCounter("",True)=0);
'
' 2002-04-13. Cactus Data ApS. CPH
' 2002-09-09. Str() sometimes fails. Replaced with CStr().
' 2005-10-21. Str(col.Count + 1) reduced to col.Count + 1.
' 2008-02-27. Optional group parameter added.
' 2010-08-04. Corrected that group key missed first row in group.

  Static col      As New Collection
  Static strGroup As String

  On Error GoTo Err_RowCounter

  If booReset = True Then
    Set col = Nothing
  ElseIf strGroup <> strGroupKey Then
    Set col = Nothing
    strGroup = strGroupKey
    col.Add 1, strKey
  Else
    col.Add col.Count + 1, strKey
  End If

  RowCounter = col(strKey)

Exit_RowCounter:
  Exit Function

Err_RowCounter:
  Select Case Err
    Case 457
      ' Key is present.
      Resume Next
    Case Else
      ' Some other error.
      Resume Exit_RowCounter
  End Select

End Function

答案 1 :(得分:0)

考虑dbms_job.submit()语句的相关计数子查询或条件DCount。下面假设您在 myTable 中有一个自动编号字段 ID

SELECT

对于SELECT t.[value], (SELECT Count(*) FROM myTable WHERE sub.[value] = t.[value] AND sub.ID <= t.ID) As value_rank FROM myTable t SELECT t.[value], DCount("*", "myTable", "[value] = " & t.[value] & " AND ID <= " & t.ID) As value_rank FROM myTable t 次查询,您需要UPDATE才能成为可更新的查询:

DCount

如果 value 是一个字符串,请用单引号换行连接:UPDATE myTable t SET t.value = DCount("*", "myTable", "[value] = " & t.value & " AND ID <= " & t.ID) 如果 value 是一个日期,请用hashtags包裹:... "[value] = '" & t.value & "' ... < / p>