DECLARE @Count BIGINT
SELECT @Count = Count(ID)
FROM Users;
SELECT TOP 50
CustUser.[ID],
CustUser.[FirstName] + ' ' + CustUser.[LastName] FirstName,
CustUser.[NickName],
CustUser.[UserName],
R.[Name] Roles
FROM
(SELECT
ROW_NUMBER() OVER(ORDER BY US.ID Desc) AS Row,
US.[ID], US.[FirstName], US.[LastName], US.[NickName],
US.[UserName], US.[Password]
FROM
[Users] US) CustUser
LEFT JOIN
Category CL ON CustUser.[LoginModeCode] = CL.CategoryCode
LEFT JOIN
Category CS ON CustUser.[StatusCode] = CS.CategoryCode
LEFT JOIN
UserRoles UR ON UR.UserID = CustUser.ID
LEFT JOIN
Roles R ON R.ID = UR.RoleID
WHERE
CustUser.ID = 3 AND
[Row] > (1 - 1) * 50
ORDER BY
FirstName
此查询返回以下输出
ID FirstName NickName UserName Roles
----------------------------------------------------------------------------
3 ram jk ram Developer
3 ram jk ram TeamLeader
角色仅在上面的行中有所不同。我正在组合两行。
但我想要这个输出
ID FirstName NickName UserName Roles
--------------------------------------------------------------
3 ram jk ram Developer, TeamLeader
答案 0 :(得分:0)
--Test Data
CREATE TABLE #Temp_table(
ID int,
FirstName nvarchar(200),
NickName nvarchar(200),
UserName nvarchar(200),
Roles nvarchar(200)
);
INSERT INTO #Temp_table VALUES (3,'ram','jk','ram','Developer');
INSERT INTO #Temp_table VALUES (3,'ram','jk','ram','TeamLeader');
INSERT INTO #Temp_table VALUES (3,'ram','jk','ram','XXXLeader');
--change temp_table to your select table
select ID,FirstName,NickName,UserName,
STUFF((
SELECT ', ' + Roles
FROM #Temp_table
FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
,1,2,'') Roles
from #Temp_table
group by ID,FirstName,NickName,UserName;