TSQL Challenge 15 - Solution by Ennor Tiegael



-- File Name: Ennor_Tiegael_TSQLChallenge_15.sql
select psq.Row,
	case when psq.[1] is not null then 'x' else '' end as [1],
	case when psq.[2] is not null then 'x' else '' end as [2],
	case when psq.[3] is not null then 'x' else '' end as [3],
	case when psq.[4] is not null then 'x' else '' end as [4],
	case when psq.[5] is not null then 'x' else '' end as [5],
	case when psq.[6] is not null then 'x' else '' end as [6],
	case when psq.[7] is not null then 'x' else '' end as [7],
	case when psq.[8] is not null then 'x' else '' end as [8],
	case when psq.[9] is not null then 'x' else '' end as [9]
from (
	select r.Row, c.Col
	from @cols c
		inner join @rows r on r.row % c.col = 0
--	inner join (select Number as Row from master.dbo.spt_values where type = 'P') r on r.row % c.col = 0
) sq
pivot (
	min(sq.Col)
	for sq.Col in ([1], [2], [3], [4], [5], [6], [7], [8], [9])
) psq
order by psq.Row;