如何在MS-Access中显示分为1行的3条记录?

Nis*_*ini 3 sql ms-access

我希望table1中的数据显示在MS access 2000中的表中

| contact_id | name                           | contact_type | 
| 297        | Primary Properties Corporation | Supplier     |
| 297        | Primary Properties Corporation | Prospect     |
| 297        | Primary Properties Corporation | Customer     |
| 298        | San Miguel Corporation         | Prospect     |
| 301        | Sulpicio Lines                 | Supplier     |
Run Code Online (Sandbox Code Playgroud)

我希望它返回:

| contact_id | name                           | contact_type    
|  297       | Primary Properties Corporation | Supplier, Prospect, Customer |
|  298       | San Miguel Corporation         | Prospect                     |
|  301       | Sulpicio Lines                 | Supplier                     | 
Run Code Online (Sandbox Code Playgroud)

我有一些方法,比如在sql中使用group concat,xml_path,但它在ms访问中不起作用.

请指导我.

Bro*_*ams 6

这是一种方式:

  1. 打开Visual Basic编辑器... 工具 - >宏 - > Visual Basic编辑器 (或AltF11)

  2. 插入模块并粘贴到此UDF中:

    'Concat returns a comma-seperated list of items
    Public Function Concat (CategoryCol As String, _
                            ItemCol     As String) As String
        Static LastCategory As String
        Static ItemList     As String
    
        If CategoryCol      = LastCategory Then
            ItemList        = ItemList & ", " & ItemCol
        Else
            LastCategory    = CategoryCol
            ItemList        = ItemCol
        End If
        Concat = ItemList
    End Function
    
    Run Code Online (Sandbox Code Playgroud)


  3. 保存项目并关闭VB编辑器

  4. 在" 查询"下,在设计视图中创建新查询.

  5. 切换到SQL视图.

  6. 粘贴在这个SQL中:

    SELECT 
        contact_id,
        name,
        LAST (Concat (contact_id, contact_type))  AS [contact_type]
    FROM
        table1
    GROUP BY
        contact_id,
        name
    ORDER BY
        contact_id
    
    Run Code Online (Sandbox Code Playgroud)


  7. 运行查询(按红色感叹号或只选择数据表视图).

  8. 完成!