我有一张这样的桌子:
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,使其如下所示:
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,但它太慢了。
或者有没有一个可以使用的函数,在查询已经打开时不会继续运行?
发布于 2013-03-26 05:26:19
Access :/中没有Group_Concat。可能没有排除VBA的解决方案。
发布于 2020-04-16 02:55:03
以下是如何使用VBA解决此问题的粗略概述;它通过对详细记录运行单个数据库查询来提高执行速度:
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
https://stackoverflow.com/questions/15624845
复制相似问题