DECLARE @Table1 TABLE(EntityID INT, AttributeID INT, OptionText VARCHAR(50))
INSERT INTO @Table1
SELECT 5030, 48, 'M'
INSERT INTO @Table1
SELECT 5030, 48, 'F'
--Begin actual working SQL
SELECT T1.EntityID,
T1.AttributeID,
STUFF(( SELECT ', ' + T2.OptionText
FROM @Table1 T2
WHERE T2.AttributeID = T1.AttributeID
AND T2.EntityID = T1.EntityID
FOR XML PATH('')
), 1, 2, '') [Attributes]
FROM @Table1 T1
GROUP BY T1.EntityID, T1.AttributeID
INSERT INTO @Table1
SELECT 5030, 48, 'M'
INSERT INTO @Table1
SELECT 5030, 48, 'F'
--Begin actual working SQL
SELECT T1.EntityID,
T1.AttributeID,
STUFF(( SELECT ', ' + T2.OptionText
FROM @Table1 T2
WHERE T2.AttributeID = T1.AttributeID
AND T2.EntityID = T1.EntityID
FOR XML PATH('')
), 1, 2, '') [Attributes]
FROM @Table1 T1
GROUP BY T1.EntityID, T1.AttributeID