2011-12-22 57 views
0

我有以下CSV格式的數據,我在訪問格式導出訪問SQL查詢來連接行

House Number | Road name | Postcode 
1 | alex road | sw2 4r 
2 | alex road | sw2 4r 
3 | alex road | sw1 2b 
4 | alex road | sw2 4r 
5 | alex road | sw2 4r 
7 | brit road | bw2 4t 
4 | brit road | bw2 4t 
6 | brit road | bw2 4t 
8 | brit road | bw2 4t 
10 | brit road | bw2 4t 

我需要一個訪問SQL查詢其可以按路名的所有記錄,記錄合計可超過20000

我需要的輸出格式如下

Road Name | House Number | Postcode 
-------------------------------------- 
alex road | 1,2,3,4,5 | sw2 4r,sw1 2b 
brit road | 7,4,6,8,10 | bw2 4t 
+1

您正在尋找類似'group_concat'的東西,但在MS Access中試試這個:http://stackoverflow.com/questions/2852892/is-there-a-group-concat-function-in-ms-access – 2011-12-22 10:26:23

回答

1

正如指出的onedaywhen在戰後初期就這樣,這是ADO容易,有an example here

樣品查詢:使用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

因爲有像在Access GROUP_CONCAT沒有這種功能

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; 

功能。所以我導入使用我的csv文件到phpMyAdmin的MySQL數據庫其內置的導入工具和使用下面的查詢將它們分組

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

,然後使用內置的導出工具phpMyAdmin的人匯出回CSV。我不知道是否有更好的方法來做到這一點。