从一列的多行创建一个逗号分隔的字符串?
我有一个像这样的访问表,
ID | UserName | CarBrand
-------------------------
0 Peter VW
1 Peter Ferrari
2 Mike Audi
3 Peter Dodge
4 Heidi BMW
5 Heidi Ford
我需要 CarBrand
字段中的名称作为报告的逗号分隔列表。
有没有办法(没有VB,也许使用COALESCE替代方案?)来创建像这样的逗号分隔字符串,而不需要 Name:
部分?
Peter: VW, Ferrari, Dodge
Mike: Audi
Heidi: BMW, Ford
由于它是用于报告,还有其他方法可以做到这一点,也许使用报告中的表达式?
I've an access table like this
ID | UserName | CarBrand
-------------------------
0 Peter VW
1 Peter Ferrari
2 Mike Audi
3 Peter Dodge
4 Heidi BMW
5 Heidi Ford
I need the names from the CarBrand
field as a comma separated list for a report.
Is there a way (without VB, maybe using a COALESCE alternative?) to create a comma-separated string like this, without the Name:
part?
Peter: VW, Ferrari, Dodge
Mike: Audi
Heidi: BMW, Ford
As it's for a report are there any other ways to do this, maybe using expressions in the report?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有 VBA,您无法在 Access 中执行此操作。 Coalesce 不存在,但您可以编写具有某些功能的 UDF,例如 http://allenbrowne .com/func-concat.html
但是,一旦使用 UDF,查询在 Access 之外就不再可行。
You cannot do this is Access without VBA. Coalesce does not exist, but you can write a UDF that has some of the functionality, for example http://allenbrowne.com/func-concat.html
However, once you use a UDF, the query is no longer viable outside Access.
创建包含唯一用户名列表的主报告。
创建包含用户名及其汽车品牌列表的子报告。在用户名上加入表格。在子表单的设计中,使用4-5列水平打印。您可以让 CarBrand 字段包含类似 =[CarBrand] & 的公式。 “,”(抱歉,最后一个将有一个不必要的逗号。它们将均匀间隔,并且如果特定用户拥有的品牌数量超出了您的报告的范围,它们将分成新的一行(这真的很难做到)如果您只是创建一个大的逗号分隔字符串。)
。
Create a main report with a unique list of usernames.
Create a sub report with a list of usernames and their Car Brands. Join the forms on the UserName. In the design of the subform, use 4-5 columns which print horizontally. You can have the CarBrand field include a formula like =[CarBrand] & ", " (Sorry the last one is going to have an unecessary comma. They will be spaced accross uniformly and will break into a new row if a particular user has more brands than can fit accross you report (Which will be real hard to do if you just create one large comma-separated string.).
No VBA involved at all.
如果您不需要逗号分隔列表中的每个值,但只需要前 3 个或 4 个左右,那么 Access 中有一个纯 SQL 解决方案。
我正在为一家拥有导师和课程的非营利组织开发数据库。大多数课程只有 1 到 2 名导师。出于显示目的,无论如何我都不能列出超过 2 或 3 个,因此我不担心用 5 个或更多导师截断异常值。
这将为每个班级获取 3 名具有最低导师 ID 的导师。
显然,需要 1 个额外步骤(未显示)将三列合并为 1 列。
If you do not need every single value in your comma delimited list, but say just up to the first 3 or 4 or so, then there is a pure SQL solution in Access.
I am working on a DB for a non-profit that has Tutors and Classes. For most classes, there is just 1 or 2 tutors. For display purposes, I can't list more than 2 or 3 anyway, so I am not worried about truncating outliers with 5 or more tutors.
This will grab the 3 tutors for each class with the lowest Tutor IDs
Obviously, 1 extra step (not shown) will be needed to combine the three columns into 1.