访问:合并类似的记录

时间:2015-08-26 08:39:00

标签: mysql

我有一个包含多个条目的表,用于唯一的UnitNo和Dates(有时不同的注释,因为它由不同的调查员填充)。我想将notes列和Investigator合并到一个注释和调查列中:

UnitNo    Date           Notes                 Investigator
----------------------------------------------------------------------------
232       13/08/15       No major issues       Andy Barney
232       13/08/15       Door Defect           Andy Barney
781       21/08/15       No defect found       John Adams
781       21/08/15       Door Defect           Andy Barney
xxx       xxxxxxxx       xxxxxxxxxxx           xxxxxxxxxx

我想基本上获得具有类似UnitNo和Date的记录的单个记录,然后将这两个字段组合起来,如果它相似则不会重复:

UnitNo    Date           Notes                 Investigator
----------------------------------------------------------------------------
232       13/08/15       No major issues       Andy Barney
                         Door Defect

781       21/08/15       No defect found       John Adams
                         Door Defect           Andy Barney

xxx       xxxxxxxx       xxxxxxxxxxx           xxxxxxxxxx

我已经检查了答案的不同位置,我找到了此代码Allen BrowneHere。 但是,当我使用ConcatRelated(xxx)表示未定义sub。[xxx]时,我收到错误。

修改

以下是我的第二部分代码:

公共函数MergeEquinoxImport()

Dim SQL1 As String
Dim qdfNew1 As QueryDef
Dim db As Database

Set db = CurrentDb

SQL1 = "SELECT Sub.[UnitNo],Sub.[Date], ConcatRelated([Notes], [TblPractise], " & _
    "[Date]='" & Sub.[Date] & "' And [UnitNo]='" & Sub.[UnitNo] & "') " & _
    "FROM (SELECT q.[UnitNo],q.[Date], FROM TblPractise " & _
    "AS q Group BY q.[UnitNo],q.[Date],) AS Sub Order " & _
    "BY Sub.[UnitNo],Sub.[Date];"

If acbDoesObjExist("Query1", acQuery) Then
    DoCmd.DeleteObject acQuery, "Query1"
End If

With db
    Set qdfNew1 = .CreateQueryDef("Query1", SQL1)
End With

结束功能

1 个答案:

答案 0 :(得分:2)

您需要与mysql中的group_concat类似的功能。

在访问中,我们没有像group_concat这样的功能:(

Bt,我们可以通过以下代码实现它:

  Select T.ColumnA
      , GetList("Select ColumnB From Table1 As T1 Where T1.ColumnA = " & [T].[ColumnA],"",", ") AS ColumnBItems
    From Table1 AS T
    Group By T.ColumnA;

请在下面提到SO问题。它包含完整的详细信息。 Microsoft Access condense multiple lines in a table