access sql query to concatenate rows

前端 未结 2 450
执念已碎
执念已碎 2020-12-22 05:36

I have following data in csv format which I exported in access format

House Number | Road name | Postcode
1 | alex road | sw2 4r
2 | alex road | sw2 4r
3 | a         


        
相关标签:
2条回答
  • 2020-12-22 06:04

    because there is no such function like group_concat in Access. so I import my csv file into phpMyAdmin mysql database using its built in import tool and use following query to group them

    SELECT  road,pcode, group_concat(house_number) 
    FROM mytable
    GROUP BY road, pcode
    

    and then later export it back to csv using phpmyadmin built in export tool. I don't know if there is a better way of doing this.

    0 讨论(0)
  • 2020-12-22 06:06

    As was pointed out by onedaywhen in an early post on SO, this is easier with ADO, there is an example here

    Sample query:

       SELECT d.DeptID, d.Department, 
              ConcatADO("SELECT FName & ' ' & SName, Address FROM Persons 
                         WHERE DeptID=" & [d].[DeptID],", "," : ") AS Who
       FROM Departments AS d INNER JOIN Persons AS p ON d.DeptID = p.DeptID
       GROUP BY d.DeptID, d.Department, 3;
    

    Function using ADO

    Function ConcatADO(strSQL As String, strColDelim, strRowDelim, ParamArray NameList() As Variant)
       Dim rs As New ADODB.Recordset
       Dim strList As String
    
       On Error GoTo Proc_Err
    
           If strSQL <> "" Then
               rs.Open strSQL, CurrentProject.Connection
               strList = rs.GetString(, , strColDelim, strRowDelim)
               strList = Mid(strList, 1, Len(strList) - Len(strRowDelim))
           Else
               strList = Join(NameList, strColDelim)
           End If
    
           ConcatADO = strList
    
       Exit Function
    
       Proc_Err:
           ConcatADO = "***" & UCase(Err.Description)
       End Function
    
    0 讨论(0)
提交回复
热议问题