Sql ms access查询需要连接编号或计数

Sql ms access查询需要连接编号或计数,sql,vba,ms-access,Sql,Vba,Ms Access,食用水果 FName ----- orange apple apple 3mango orange orange apple mango 我想要一个计算结果的查询 orange-1 apple-1 apple-2 mango-1 orange-2 orange-3 apple-3 mango-2 如果添加ID(自动编号),则可以将水果名称作为组键使用此功能: Public Function RowCounter( _ ByVal strKey As String, _ ByVal b

食用水果

FName
-----
orange
apple
apple
3mango
orange
orange
apple
mango
我想要一个计算结果的查询

orange-1
apple-1
apple-2
mango-1
orange-2
orange-3
apple-3
mango-2
如果添加ID(自动编号),则可以将水果名称作为组键使用此功能:

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
公共函数行计数器(_
比瓦尔·斯特尔基作为字符串_
ByVal booReset为布尔值_
可选ByVal STRGROUPPEY(字符串形式)_
只要
'在选择、追加或创建查询中生成连续的行ID
'具有自动复位的可能性。
'可以选择传递分组键以重置行计数
'对于每个组键。
'
'用法(典型的选择查询):
'选择行计数器(CStr([ID]),False)作为行ID*
“来自tblSomeTable
'其中(RowCounter(CStr([ID]),False)RowCounter(“,True));
'
'用法(使用组密钥):
'选择行计数器(CStr([ID]),False,CStr[GroupID])作为行ID*
“来自tblSomeTable
'其中(RowCounter(CStr([ID]),False)RowCounter(“,True));
'
'Where语句在运行查询时重置计数器
'和用于浏览选择查询。
'
'用法(典型追加查询、手动重置):
' 1. 手动重置计数器:
'调用行计数器(vbNullString,False)
' 2. 运行查询:
'插入到tblTemp(RowID)
'选择行计数器(CStr([ID]),False)作为行ID*
“来自TBL可计量表;
'
'用法(典型追加查询、自动重置):
'插入到tblTemp(RowID)
'选择行计数器(CStr([ID]),False)作为行ID*
“来自tblSomeTable
'其中(行计数器(“,True)=0);
'
' 2002-04-13. 仙人掌数据。CPH
' 2002-09-09. Str()有时会失败。替换为CStr()。
' 2005-10-21. Str(列数+1)减少为列数+1。
' 2008-02-27. 添加了可选的组参数。
' 2010-08-04. 更正了组键缺少组中的第一行。
静态col作为新集合
静态strGroup作为字符串
关于错误转到错误行计数器
如果booReset=True,则
设置col=Nothing
ElseIf strGroup strGroupKey然后
设置col=Nothing
strGroup=strGroupKey
第1列,斯特基
其他的
列加列计数+1,strKey
如果结束
行计数器=列(strKey)
退出行计数器:
退出功能
错误计数器:
选择大小写错误
案例457
“钥匙在。
下一步继续
其他情况
“还有别的错误。
恢复退出行计数器
结束选择
端函数
有关典型用法,请参阅联机注释。

如果您添加了一个ID(自动编号),则可以将水果名称作为组键使用此功能:

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
公共函数行计数器(_
比瓦尔·斯特尔基作为字符串_
ByVal booReset为布尔值_
可选ByVal STRGROUPPEY(字符串形式)_
只要
'在选择、追加或创建查询中生成连续的行ID
'具有自动复位的可能性。
'可以选择传递分组键以重置行计数
'对于每个组键。
'
'用法(典型的选择查询):
'选择行计数器(CStr([ID]),False)作为行ID*
“来自tblSomeTable
'其中(RowCounter(CStr([ID]),False)RowCounter(“,True));
'
'用法(使用组密钥):
'选择行计数器(CStr([ID]),False,CStr[GroupID])作为行ID*
“来自tblSomeTable
'其中(RowCounter(CStr([ID]),False)RowCounter(“,True));
'
'Where语句在运行查询时重置计数器
'和用于浏览选择查询。
'
'用法(典型追加查询、手动重置):
' 1. 手动重置计数器:
'调用行计数器(vbNullString,False)
' 2. 运行查询:
'插入到tblTemp(RowID)
'选择行计数器(CStr([ID]),False)作为行ID*
“来自TBL可计量表;
'
'用法(典型追加查询、自动重置):
'插入到tblTemp(RowID)
'选择行计数器(CStr([ID]),False)作为行ID*
“来自tblSomeTable
'其中(行计数器(“,True)=0);
'
' 2002-04-13. 仙人掌数据。CPH
' 2002-09-09. Str()有时会失败。替换为CStr()。
' 2005-10-21. Str(列数+1)减少为列数+1。
' 2008-02-27. 添加了可选的组参数。
' 2010-08-04. 更正了组键缺少组中的第一行。
静态col作为新集合
静态strGroup作为字符串
关于错误转到错误行计数器
如果booReset=True,则
设置col=Nothing
ElseIf strGroup strGroupKey然后
设置col=Nothing
strGroup=strGroupKey
第1列,斯特基
其他的
列加列计数+1,strKey
如果结束
行计数器=列(strKey)
退出行计数器:
退出功能
错误计数器:
选择大小写错误
案例457
“钥匙在。
下一步继续
其他情况
“还有别的错误。
恢复退出行计数器
结束选择
端函数

有关典型用法,请参阅联机注释。

在MS SQL Server中是一个简单的
行号()窗口函数,因此您只需要在MS Access中使用等效函数即可。但为此,您需要在表中设置一个
id
列,因为您需要进行自连接

解决方案还包括:


在MS SQL Server中,将是一个简单的
ROW\u NUMBER()
window函数,因此您只需要在MS Access中使用等效函数。但为此,您需要在表中设置一个
id
列,因为您需要进行自连接

解决方案还包括:


订单是如何定义的?有ID列吗?Table Fruits FName------橙苹果芒果橙苹果芒果橙苹果芒果------我想要一个结果为couting的查询。orange-1 apple-1 apple-2 mango-1 orange-2 orange-3 apple-3 mango-2Hi,如果您的问题得到了回答,请标记“是答案”。订单是如何定义的?有ID列吗?Table Fruits FName------橙苹果芒果橙苹果芒果橙苹果芒果------我想要一个结果为couting的查询。orange-1 apple-1 apple-2 mango-1 orange-2 orange-3 apple-3 mango-2Hi,如果您的问题得到了回答,请标记“是答案”。