首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Access中连接记录和分组依据

在Access中连接记录和分组依据
EN

Stack Overflow用户
提问于 2013-03-26 05:11:17
回答 2查看 31.3K关注 0票数 8

我有一张这样的桌子:

代码语言:javascript
复制
title               part                   desc
Blah This           1                      This begins the
Blah This           2                      example table.
Some Record         1                      Hello
Another             1                      This text extends a bit
Another             2                      further so it is in
Another             3                      another record in the
Another             4                      table

在Access中,我希望构建一个按title分组并连接desc字段的查询/SQL,使其如下所示:

代码语言:javascript
复制
title              desc
Blah This          This begins the example table.
Some Record        Hello
Another            This text extends a bit further so it is in another record in the table

FOR XML PATH似乎不能在Access中工作,只能在SQL Server中工作。我在How to improve efficiency of this query & VBA?这里尝试过VBA,但它太慢了。

或者有没有一个可以使用的函数,在查询已经打开时不会继续运行?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-26 05:26:19

Access :/中没有Group_Concat。可能没有排除VBA的解决方案。

这里有一种可能:Concatenating Rows through a query

票数 5
EN

Stack Overflow用户

发布于 2020-04-16 02:55:03

以下是如何使用VBA解决此问题的粗略概述;它通过对详细记录运行单个数据库查询来提高执行速度:

代码语言:javascript
复制
Set rsParent = CodeDb.OpenRecordset("SELECT * FROM MainTable ORDER BY HeaderID")
Set rsDetail = CodeDb.OpenRecordset("SELECT * FROM DetailTable ORDER BY HeaderID")
Do Until rsParent.EOF
  ...
  myString = rsParent!MainHeaderName & AggregateDetails(rsDetail, rsParent!HeaderID)
  rsParent.MoveNext
Loop
...

Function AggregateDetails(rsDetail as Recordset, HeaderID as String) as String
   Dim DetailString as String

   Do While rsDetail!HeaderID = HeaderID
      DetailString = DetailString & ", " & rsDetail!DetailName
      rsDetail.MoveNext
      If rsDetail.EOF Then Exit Do
   Loop
   AggregateDetails = DetailString
End Function
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15624845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档