将多个值合并到SSM中的单元格中

发布于 2025-02-12 18:36:36 字数 1804 浏览 1 评论 0原文

我试图将数据库中的一些数据汇总到带有以下查询的凝聚表中。问题在于用户可以属于多个组,因此,除了组列外,任何情况下,用户数据都会在新行中复制,所有内容都相同。我想将这些组合并为单个逗号分隔的值。我尝试了几种不同的事情,例如String_agg和Cocece,但是数据链接在许多不同的表和数据类型上,这些问题我遇到的问题可以正常工作。

查询:

SELECT FirstName, LastName, Email, 
--POSITION
JSON_VALUE(p.NameJson,'$[0].Text') AS Position, 
--DEPARTMENT
JSON_VALUE(d.NameJson,'$[0].Text') AS Department, 
--USER GROUPS
JSON_VALUE(ug.NameJson, '$[0].Text') AS Groups,
--ROLES
JSON_VALUE(sr.TitleJson, '$[0].Text') AS Roles
    FROM AspNetUsers a
    LEFT JOIN Positions p 
    ON a.PositionId = p.Id
    LEFT JOIN Departments d
    ON a.DepartmentId = d.Id
    LEFT JOIN UserRoles ur
    ON a.Id = ur.UserId
    LEFT JOIN UserGroupUsers ugu
    ON a.Id = ugu.User_Id
    LEFT JOIN UserGroups ug
    ON ugu.UserGroup_Id = ug.Id
    LEFT JOIN SecurityRoles sr
    ON ur.RoleId = sr.Id

ORDER BY LastName desc

编辑: 这是实际数据,有点编辑。

firstNamelastName电子邮件位置部门部门角色
首次电子邮件City城市职员城市职员店员管理员
第一个电子邮件电子邮件城市店员City店员DH_CITY店员
第一个上一封CityCityCity City CityCity City Group groupArmissionator,

此处的目标是返回一排:

first Namame:first Namame:first NameLastName电子邮件位置部门部门角色
首先发送电子邮件City CityCity CityCity City店员,DH_CITY店员,城市文员组管理员

I am trying to aggregate some data from our database into a cohesive table with the below query. The issue is that users can belong to more than one group, so any time that happens, the user data is duplicated on a new row with everything the same aside from the group column. I'd like to consolidate those groups into a single, comma-separated value. I've tried a few different things like STRING_AGG and COALESCE, but the data is linked across so many different tables and data types that I'm having issues getting that to work.

Query:

SELECT FirstName, LastName, Email, 
--POSITION
JSON_VALUE(p.NameJson,'$[0].Text') AS Position, 
--DEPARTMENT
JSON_VALUE(d.NameJson,'$[0].Text') AS Department, 
--USER GROUPS
JSON_VALUE(ug.NameJson, '$[0].Text') AS Groups,
--ROLES
JSON_VALUE(sr.TitleJson, '$[0].Text') AS Roles
    FROM AspNetUsers a
    LEFT JOIN Positions p 
    ON a.PositionId = p.Id
    LEFT JOIN Departments d
    ON a.DepartmentId = d.Id
    LEFT JOIN UserRoles ur
    ON a.Id = ur.UserId
    LEFT JOIN UserGroupUsers ugu
    ON a.Id = ugu.User_Id
    LEFT JOIN UserGroups ug
    ON ugu.UserGroup_Id = ug.Id
    LEFT JOIN SecurityRoles sr
    ON ur.RoleId = sr.Id

ORDER BY LastName desc

EDIT:
Here's the actual data, redacted a bit.

firstNamelastNameemailpositiondepartmentgroupsroles
FirstLastEmailCity ClerkCity ClerkCity ClerkAdministrator
FirstLastEmailCity ClerkCity ClerkDH_City ClerkAdministrator
FirstLastEmailCity ClerkCity ClerkCity Clerk GroupAdministrator

The goal here is to return one row that reads:

firstNamelastNameemailpositiondepartmentgroupsroles
FirstLastEmailCity ClerkCity ClerkCity Clerk, DH_City Clerk, City Clerk GroupAdministrator

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文