Ms Access 查询:通过查询连接行
假设我在 Ms Access 中有包含以下信息的表:
ColumnA ColumnB
1 abc
1 pqr
1 xyz
2 efg
2 hij
3 asd
我的问题是,如何将第二列中的值连接到基于第一列的行值。我想要的查询结果如下:
ColumnA ColumnB
1 abc, pqr, xyz
2 efg, hij
3 asd
我想通过查询来实现。有人可以帮助我实现这个目标吗?
Suppose I have table in Ms Access with following information:
ColumnA ColumnB
1 abc
1 pqr
1 xyz
2 efg
2 hij
3 asd
My question is, how can I concatenate the values in the second column to a row value based on the first column. The query results that I want is as follows:
ColumnA ColumnB
1 abc, pqr, xyz
2 efg, hij
3 asd
I want to achieve this through a query. Can someone help me attain this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要一个函数来进行串联。
Microsoft Access 压缩表格中的多行
使用您的数据的示例:
You need a function to do the concatenation.
Microsoft Access condense multiple lines in a table
Example using your data:
这是一个关于如何通过调用函数从 SQL 内部执行此操作的优秀链接。说明非常清晰&该函数是为您编写的,因此您只需复制、粘贴即可;去。即使不懂 VB 的人也可以轻松实现:
连接相关记录中的值
Here's an outstanding link re: how to do this from within SQL by calling a function. The instructions are exceptionally clear & the function is written for you so you can just copy, paste & go. Even someone with no knowledge of VB can easily implement it:
Concatenate values from related records
这可能很难获得。如果您必须在查询而不是函数中执行此操作,那么您将遇到的问题是可以连接到一列中的行数的限制。到目前为止,我发现实现此目的的唯一方法是通过 iif 语句。
返回:
这将仅返回第一个和最后一个,但我确信只需做一些工作,您就可以计算出 Choose 函数,但就像我说的,您必须为要添加的每个附加项目添加更多 iif 语句,因此存在限制。
this can be very difficult to obtain. If you MUST do it in a query and not a function, the problem that you will run into is the limit of the number of rows you can concatenate into one column. So far the only way that i have found to achieve this is via iif statements.
returns:
This will return the first and the last only, but I'm sure with a little work you could work out the Choose function, but like I said you would have to add more iif statements for each additional item you want to add, hence the limitation.
该表可以有一个序列列,这为其提供了 ColumnA 序列的唯一主键:
并且可以创建交叉表:
然后最终查询可以组合列并删除最后一个逗号:
要自动填充序列,请执行以下操作可以使用VBA代码:
The table could have a sequence column, which gives it a unique primary key of ColumnA-sequence:
And a Crosstab could be created:
Then a final query can combine the columns and remove the last comma:
To automate filling in the sequence, the following VBA code can be used: