从一列的多行创建一个逗号分隔的字符串?

发布于 2024-12-09 14:24:26 字数 481 浏览 0 评论 0原文

我有一个像这样的访问表,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凡间太子 2024-12-16 14:24:26

如果没有 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.

萌梦深 2024-12-16 14:24:26

创建包含唯一用户名列表的主报告。

创建包含用户名及其汽车品牌列表的子报告。在用户名上加入表格。在子表单的设计中,使用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.

暮年 2024-12-16 14:24:26

如果您不需要逗号分隔列表中的每个值,但只需要前 3 个或 4 个左右,那么 Access 中有一个纯 SQL 解决方案。

我正在为一家拥有导师和课程的非营利组织开发数据库。大多数课程只有 1 到 2 名导师。出于显示目的,无论如何我都不能列出超过 2 或 3 个,因此我不担心用 5 个或更多导师截断异常值。

这将为每个班级获取 3 名具有最低导师 ID 的导师。

Select JTC1.ClassID, Min(JTC1.TID1) as TutorID1,
  Min(JTC1.TID2) as TutorID2,
  Min(JTC1.TID3) as TutorID3 
from (
  Select distinct TC1.ClassID,
    TC1.TutorID as TID1,
    TC2.TutorID as TID2,
    TC3.TutorID as TID3 
  from ((
   Classes C 
   Left Join TutorClasses TC1 
     on C.ClassID = TC1.ClassID)
   Left Join TutorClasses TC2 
     on TC1.ClassID = TC2.ClassID and TC1.TutorID < TC2.TutorID
   )
   Left Join TutorClasses TC3
     on TC2.ClassID = TC3.ClassID and TC2.TutorID < TC3.TutorID
   ) as JTC1
Group by JTC1.ClassID

显然,需要 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

Select JTC1.ClassID, Min(JTC1.TID1) as TutorID1,
  Min(JTC1.TID2) as TutorID2,
  Min(JTC1.TID3) as TutorID3 
from (
  Select distinct TC1.ClassID,
    TC1.TutorID as TID1,
    TC2.TutorID as TID2,
    TC3.TutorID as TID3 
  from ((
   Classes C 
   Left Join TutorClasses TC1 
     on C.ClassID = TC1.ClassID)
   Left Join TutorClasses TC2 
     on TC1.ClassID = TC2.ClassID and TC1.TutorID < TC2.TutorID
   )
   Left Join TutorClasses TC3
     on TC2.ClassID = TC3.ClassID and TC2.TutorID < TC3.TutorID
   ) as JTC1
Group by JTC1.ClassID

Obviously, 1 extra step (not shown) will be needed to combine the three columns into 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文