2013-05-07 55 views
1

我有一张桌子。如何在MS Access 2007中编写查询以在多个列中显示多个值?

表名:tblRAC

Examiner Subj scheme pap_code moderator pap_accessed pap_moderated 
Kamble DTE EJ1E 12116 mahulkar  25   5 
kamble AEL DE4E 12110 Patil  2    2 
kamble DTE CO3C 12064 Nale   37   20 
kamble DTE IF3E 12064     01    
Kamble DTE IL1E 12116 kulkarni  35   5 

如何写MS Access 2007中查询得到如下输出。

subj pap_Code scheme  pap_accessed moderator 
DTE  12116 EJ1E,IL1E  60   mahulkar,kulkarni 
DTE  12064 CO3C,IF3E  38   Nale 
AEL  12110 DE4E   02   Patil 

        Total 100  
+0

http://www.rogersaccesslibrary.com/forum/generic-function-to-concatenate-child-records_topic16&SID=453fabc6-b3z9-34z6zb14-a78f832z-19z89a2c.html你想要的是像一般常见的功能称为'group_concat',但Access不支持它。 – Scotch 2013-05-07 05:19:54

回答

1

我实际上决定发布这个答案而不是评论,以使它更容易访问。

你想要的就是其他DBMS所提供的(我不太喜欢使用),它被称为GROUP_CONCAT。我编辑了你的标题,以使未来的搜索者更加明显(希望你不介意)。

由于Access在SQL语法中不支持GROUP_CONCAT,所以您可以编写一些时髦的SQL来希望复制它或只使用VBA。

以下是您可以使用的示例GROUP CONCAT VBA函数。发现here(我没有写)。

Function Concatenate(pstrSQL As String, _ 
     Optional pstrDelim As String = ", ", _ 
     Optional pstrLastDelim As String = "") _ 
     As Variant 
' Created by Duane Hookom, 2003 
' this code may be included in any application/mdb providing 
' this statement is left intact 
' example 
' tblFamily with FamID as numeric primary key 
' tblFamMem with FamID, FirstName, DOB,... 
' return a comma separated list of FirstNames 
' for a FamID 
' John, Mary, Susan 

' ======= in a Query ========================= 
' SELECT FamID, 
' Concatenate("SELECT FirstName FROM tblFamMem 
' WHERE FamID =" & [FamID]) as FirstNames 
' FROM tblFamily 
' ============================================ 

' to get a return like Duane, Laura, Jake, and Chelsey 

' ======= in a Query ========================= 
' SELECT FamID, 
' Concatenate("SELECT FirstName FROM tblFamMem 
' WHERE FamID =" & [FamID], ",",", and ") as FirstNames 
' FROM tblFamily 
' ============================================ 

' If FamID is a string rather than numeric, 
' it will need to be delimited with quotes 

' ======= in a Query ========================= 
' SELECT FamID, 
' Concatenate("SELECT FirstName FROM tblFamMem 
' WHERE FamID =""" & [FamID] & """", ",",", and ") as FirstNames 
' FROM tblFamily 
' ============================================ 


'======For DAO uncomment next 4 lines======= 
'====== comment out ADO below ======= 
    Dim db As DAO.Database 
    Dim rs As DAO.Recordset 
    Set db = CurrentDb 
    Set rs = db.OpenRecordset(pstrSQL) 

'======For ADO uncomment next two lines===== 
'====== comment out DAO above ====== 
    'Dim rs As New ADODB.Recordset 
    'length before last concatenation 
    Dim intLenB4Last As Integer 
    'rs.Open pstrSQL, CurrentProject.Connection, _ 
    adOpenKeyset, adLockOptimistic 
    Dim strConcat As String 'build return string 
    With rs 
     If Not .EOF Then 
      .MoveFirst 
      Do While Not .EOF 
       intLenB4Last = Len(strConcat) 
       strConcat = strConcat & _ 
       .Fields(0) & pstrDelim 
       .MoveNext 
      Loop 
     End If 
     .Close 
    End With 
    Set rs = Nothing 
'====== uncomment next line for DAO ======== 
    Set db = Nothing 
    If Len(strConcat) > 0 Then 
     strConcat = Left(strConcat, _ 
      Len(strConcat) - Len(pstrDelim)) 
     If Len(pstrLastDelim) > 0 Then 
      strConcat = Left(strConcat, _ 
       intLenB4Last - Len(pstrDelim) - 1) _ 
       & pstrLastDelim & Mid(strConcat, intLenB4Last + 1) 
     End If 
    End If 
    If Len(strConcat) > 0 Then 
     Concatenate = strConcat 
    Else 
     Concatenate = Null 
    End If 
End Function 
+0

Sory我没有得到任何东西:( – 2013-05-07 05:30:42

+0

你需要在你的项目中创建一个模块,然后将这个代码复制到它里面,然后你可以在你的查询中使用'Concatenate()'作为例子显示 – Scotch 2013-05-07 05:33:15

+0

它非常强悍的plzz can你根据我给定的数据写一个模块 – 2013-05-07 05:36:16

相关问题