将多个值合并到SSM中的单元格中
我试图将数据库中的一些数据汇总到带有以下查询的凝聚表中。问题在于用户可以属于多个组,因此,除了组列外,任何情况下,用户数据都会在新行中复制,所有内容都相同。我想将这些组合并为单个逗号分隔的值。我尝试了几种不同的事情,例如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
编辑: 这是实际数据,有点编辑。
firstName | lastName | 电子邮件 | 位置 | 部门 | 部门 | 角色 |
---|---|---|---|---|---|---|
首次 | 电子邮件 | City | 城市职员 | 城市 | 职员店员 | 管理员 |
第一个 | 电子邮件 | 电子邮件 | 城市店员 | City | 店员DH_CITY | 店员 |
第一个 | 上一封 | City | City | City City City | City City Group group | Armissionator, |
此处的目标是返回一排:
first Namame:first Namame:first Name | LastName | 电子邮件 | 位置 | 部门 | 部门 | 角色 |
---|---|---|---|---|---|---|
首先 | 发送 | 电子邮件 | City City | City City | City 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.
firstName | lastName | position | department | groups | roles | |
---|---|---|---|---|---|---|
First | Last | City Clerk | City Clerk | City Clerk | Administrator | |
First | Last | City Clerk | City Clerk | DH_City Clerk | Administrator | |
First | Last | City Clerk | City Clerk | City Clerk Group | Administrator |
The goal here is to return one row that reads:
firstName | lastName | position | department | groups | roles | |
---|---|---|---|---|---|---|
First | Last | City Clerk | City Clerk | City Clerk, DH_City Clerk, City Clerk Group | Administrator |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论