Przykład:
UserId/Alias
1/MrX
1/MrY
1/MrA
2/Abc
2/Xyz
Chcę wynik zapytania w następującym formacie:
UserId/Alias
1/ MrX, MrY, MrA
2/ Abc, Xyz
1 odpowiedź
Możesz użyć funkcji z COALESCE.
CREATE FUNCTION [dbo].[GetAliasesById]
(
@userID int
)
RETURNS varchar(max)
AS
BEGIN
declare @output varchar(max)
select @output = COALESCE(@output + ', ', '') + alias
from UserAliases
where userid = @userID
return @output
END
GO
SELECT UserID, dbo.GetAliasesByID(UserID)
FROM UserAliases
GROUP BY UserID
GO