add remove split language show hidden hide
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
15 rows affected
No Painter_India Painter_Nepal Washer_India Washer_Nepal Officer_India Officer_Nepal
1 John Jerry MArk Bibin Angel Peter
2 Lilly Jemi George Jerry Bosan Abrahm
3 null Curly null Moe null Larry
No Painter_India Painter_Nepal Washer_India Washer_Nepal Officer_India Officer_Nepal
1 John Jerry MArk Bibin Angel Peter
2 Lilly Jemi George Jerry Bosan Abrahm
3 null Curly null Moe null Larry
Warning: Null value is eliminated by an aggregate or other SET operation.

No Officer_India Officer_Nepal Painter_India Painter_Nepal Washer_India Washer_Nepal
1 Angel Peter John Jerry MArk Bibin
2 Bosan Abrahm Lilly Jemi George Jerry
3 null Larry null Curly null Moe

    WITH CTE_NumberedPositions AS (
        SELECT
            ROW_NUMBER() OVER(PARTITION BY Position, [Group] ORDER BY SLNo) AS No,
            CONCAT(Position, '_', [Group]) AS Combined,
            Name
        FROM #Temp
    )
    SELECT
        No, 
        [Officer_India], [Officer_Nepal], [Painter_India], [Painter_Nepal], [Washer_India], [Washer_Nepal]
    FROM CTE_NumberedPositions
    PIVOT (
        MAX(Name)
        FOR Combined IN ([Officer_India], [Officer_Nepal], [Painter_India], [Painter_Nepal], [Washer_India], [Washer_Nepal])
    ) PVT
    ORDER BY PVT.No


No Officer_India Officer_Nepal Painter_India Painter_Nepal Washer_India Washer_Nepal
1 Angel Peter John Jerry MArk Bibin
2 Bosan Abrahm Lilly Jemi George Jerry
3 null Larry null Curly null Moe

    WITH CTE_NumberedPositions AS (
        SELECT *,
            ROW_NUMBER() OVER(PARTITION BY Position, [Group] ORDER BY SLNo) AS No
        FROM #Temp
    )
    SELECT
        No,
        MAX(CASE WHEN Position = 'Officer' AND [Group] = 'India' THEN Name END) AS [Officer_India],
        MAX(CASE WHEN Position = 'Officer' AND [Group] = 'Nepal' THEN Name END) AS [Officer_Nepal],
        MAX(CASE WHEN Position = 'Painter' AND [Group] = 'India' THEN Name END) AS [Painter_India],
        MAX(CASE WHEN Position = 'Painter' AND [Group] = 'Nepal' THEN Name END) AS [Painter_Nepal],
        MAX(CASE WHEN Position = 'Washer' AND [Group] = 'India' THEN Name END) AS [Washer_India],
        MAX(CASE WHEN Position = 'Washer' AND [Group] = 'Nepal' THEN Name END) AS [Washer_Nepal]
    FROM CTE_NumberedPositions
    GROUP BY No
    ORDER BY No

Warning: Null value is eliminated by an aggregate or other SET operation.