8

我有一张这样的桌子:

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 中,我希望为 GROUP BY 构建一个查询/SQLtitle并连接该desc字段,使其看起来像这样:

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

仅使用 SQL(无 VBA/脚本)如何做到这一点?FOR XML PATH似乎在 Access 中不起作用,只有 SQL Server。我在这里尝试过 VBA如何提高此查询和 VBA 的效率?,但它实在是太慢了。

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

4

2 回答 2

5

Access :/ 中没有 Group_Concat。可能没有排除 VBA 的解决方案。
这是一种可能:通过查询连接行

于 2013-03-25T21:26:19.140 回答
1

以下是如何使用 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
于 2020-04-15T18:55:03.310 回答