sql server get multiple row in columns code example
Example 1: sql select merge multiple values
DECLARE @combinedString VARCHAR(MAX)
SELECT @combinedString = COALESCE(@combinedString + ', ', '') + column_name
FROM table_name
WHERE ID = 1234
SELECT @combinedString as StringValue
Example 2: sql roll up rows into columns
SELECT
SS.SEC_NAME,
STUFF((SELECT '; ' + US.USR_NAME
FROM USRS US
WHERE US.SEC_ID = SS.SEC_ID
ORDER BY USR_NAME
FOR XML PATH('')), 1, 1, '') [SECTORS/USERS]
FROM SALES_SECTORS SS
GROUP BY SS.SEC_ID, SS.SEC_NAME
ORDER BY 1