我需要帮助关于我所附的截图。
场景:一个用户可以有多个角色。所以在屏幕截图中,相同的userid有不同的rowid和rolename。我想把它们显示在一行中,就像我在屏幕截图中所显示的那样。
SELECT
ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS Id,
U.UserId, U.FirstName, U.LastName,
(U.FirstName + ' ' + U.LastName) AS FullName,
u.email, u.PhoneNumber, u.Username,
COALESCE(RR.RoleId, PU.RoleId, sbr.RoleId) AS RoleId,
COALESCE(RR.RoleName, R.RoleName,sbr.RoleName) AS RoleName,
COALESCE(RR.RoleType, R.RoleType,sbr.RoleType) AS RoleType, u.[Image]
FROM
[User] U
LEFT JOIN
PlatformUser PU ON PU.UserId = u.UserId AND PU.IsDeleted <> 1
LEFT JOIN
Role R ON R.RoleId = PU.RoleId
LEFT JOIN
UserToSchool UTS ON UTS.UserId = u.UserId AND UTS.IsDeleted <> 1
LEFT JOIN
Role RR ON RR.RoleId = UTS.RoleId
LEFT JOIN
dbo.UserToSchoolBranch usb ON usb.UserId = u.UserId AND usb.IsDeleted <> 1
LEFT JOIN
dbo.Role sbr ON sbr.RoleId = usb.RoleId
WHERE
(RR.RoleType = @pRoleType OR R.RoleType = @pRoleType OR sbr.RoleType = @pRoleType)
AND ISNULL(u.isdeleted, 0) <> 1
1条答案
按热度按时间xghobddn1#
如果您的查询为cte,那么您可以使用
FOR XML PATH()
要获得一行:或者你可以试着用
String_AGG
(sql server 2017):