我希望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访问中不起作用.
请指导我.
这是一种方式:
打开Visual Basic编辑器... 工具 - >宏 - > Visual Basic编辑器 (或AltF11)
插入模块并粘贴到此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)
保存项目并关闭VB编辑器
在" 查询"下,在设计视图中创建新查询.
切换到SQL视图.
粘贴在这个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)
运行查询(按红色感叹号或只选择数据表视图).
完成!
归档时间: |
|
查看次数: |
112 次 |
最近记录: |