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

Posted

技术标签:

【中文标题】ms-access 查询需要连接编号或计数【英文标题】:ms-access query needs to concatenate the numbering or counting 【发布时间】:2017-10-24 10:28:21 【问题描述】:

餐桌上的水果

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 列吗? Table Fruits FName ----- orange apple apple mango orange orange apple mango ----- 我想要一个查询结果与计算。橙1 苹果1 苹果2 芒果1 橙2 橙3 苹果3 芒果2 您好,如果您的问题已为您解答,请标记“已回答”。 【参考方案1】:

在 MS SQL Server 中将是一个简单的ROW_NUMBER() 窗口函数,因此您只需要在 MS Access 中等效。但为此,您需要在表中添加一个id 列,因为您需要进行自加入

解决方案也在这里:Achieving ROW_NUMBER / PARTITION BY in MS Access

declare @tbl as table (
    id int
    ,fname varchar(15)
)

insert into @tbl values (1,'orange')
insert into @tbl values (2,'apple')
insert into @tbl values (3,'apple')
insert into @tbl values (4,'mango')
insert into @tbl values (5,'orange')
insert into @tbl values (6,'orange')
insert into @tbl values (7,'apple')
insert into @tbl values (8,'mango')

SELECT 
    t1.id
    ,t1.fname
    ,COUNT(*) AS [Ino Seq]
FROM 
    @tbl AS t1
    INNER JOIN
    @tbl AS t2
        ON t2.fname = t1.fname
        and t1.id >= t2.id
GROUP BY t1.id, t1.fname

【讨论】:

【参考方案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

查看内嵌 cmets 了解典型用法。

【讨论】:

以上是关于ms-access 查询需要连接编号或计数的主要内容,如果未能解决你的问题,请参考以下文章

提高计算 MS-Access 中大型数据集 7 天滚动平均值的查询的性能

MS-Access 中的多个日期范围

为啥 MS-Access 中的 Teradata 查询比 SQL Server 更快

MS-Access:在分隔符上拆分内容并与其他表连接

mysql vs ms-access DB上的sql查询左连接

ADODBAPI 与数据库的打开连接数