TSQL Challenge 15 - Solution by Jesse Mclain



-- File Name: jesse_mclain_tsqlchallenge_15.sql
SELECT 
	"col" = Row,
	"1" = CASE WHEN [1] = 0 THEN 'x' ELSE '' END,
	"2" = CASE WHEN [2] = 0 THEN 'x' ELSE '' END,
	"3" = CASE WHEN [3] = 0 THEN 'x' ELSE '' END,
	"4" = CASE WHEN [4] = 0 THEN 'x' ELSE '' END,
	"5" = CASE WHEN [5] = 0 THEN 'x' ELSE '' END,
	"6" = CASE WHEN [6] = 0 THEN 'x' ELSE '' END,
	"7" = CASE WHEN [7] = 0 THEN 'x' ELSE '' END,
	"8" = CASE WHEN [8] = 0 THEN 'x' ELSE '' END,
	"9" = CASE WHEN [9] = 0 THEN 'x' ELSE '' END
FROM (
	SELECT
		R.Row,
		C.Col,
		Divisible = R.Row % C.Col
	FROM @Rows R
	CROSS JOIN @Cols C
) AS C
PIVOT (SUM(Divisible) FOR Col IN ([1], [2], [3], [4], [5], [6], [7], [8], [9])) AS Pvt
ORDER BY Row

Did you find something incorrect/wrong with this solution? Take a few seconds to Report It.

Did you understand how this solution work? If you find it difficult to understand, you can Request an Explanation or you can Write an explanation to help others better understand this solution.