Concatenate rows into 1 - access 2007

前端 未结 2 733
礼貌的吻别
礼貌的吻别 2020-12-22 10:31

I\'m making a database in Access 2007, and currently I\'m struggling with a query. In only one column, it is possible to cose multiple answers. So, when I open the query I g

相关标签:
2条回答
  • 2020-12-22 10:59

    "Standard" SQL won't do this, I think. There may be some features in Access to return an array with column values... but unless someone else comes up with a cool suggestion based on such a capability, I'd say "no".

    This kind of processing normally has to be done in the programming environment that called the SQL. Are you using some kind of report generator such as BIRT or are you doing "straight" JDBC? If the latter, then you can simply loop through your ResultSet to put that compound column together.

    0 讨论(0)
  • 2020-12-22 11:23

    How about:

    The code sample below will return a delimited list from either SQL or string input. This is how to use it in a query:

       SELECT documents.MembersOnly, 
           ConcatList("SELECT FName From Persons WHERE Member=True",":") AS Who, 
           ConcatList("",":","Mary","Joe","Pat?") AS Others
       FROM documents;
    

    Or

       SELECT tc.Company, 
              ConcatList("SELECT Contract 
                          FROM tblservices 
                          WHERE CompanyKey=" & tc.[CompanyKey],", ") AS Contracts
       FROM tblCompanies tc
    

    Sample Code

       Function ConcatList(strSQL As String, strDelim, _
                ParamArray NameList() As Variant)
       ''Reference: Microsoft DAO x.x Object Library
       Dim db As Database
       Dim rs As DAO.Recordset
       Dim strList As String
    
       Set db = CurrentDb
    
       If strSQL <> "" Then
           Set rs = db.OpenRecordset(strSQL)
    
           Do While Not rs.EOF
               strList = strList & strDelim & rs.Fields(0)
               rs.MoveNext
           Loop
    
           strList = Mid(strList, Len(strDelim) + 1)
       Else
    
           strList = Join(NameList, strDelim)
       End If
    
       ConcatList = strList
    
       End Function
    

    From: http://wiki.lessthandot.com/index.php/Concatenate_a_List_into_a_Single_Field_%28Column%29


    The link above now includes the ADO version below, which I include here as @onedaywhen never added a version.

    This is faster and easier, but a lot more sensitive about field (column) names. You need to ensure that they are not reserved words. Another advantage of the ADO version is that you are not limited in the number of columns to be concatenated:

    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 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)
提交回复
热议问题