TSQL Challenge 15 - Solution by Fabien Contaminard (1)



-- File Name: fabien_contaminard_tsqlchallenge_15_1.sql
WITH Divide(Col, Row, Match) AS
(
  SELECT Col, Row, 'x'
    -- the join predicate was almost given in hte challenge statement
    FROM @Cols INNER JOIN @Rows ON Row%Col = 0
)
  SELECT Row,
         coalesce([1], '') as [1],
         coalesce([2], '') as [2],
         coalesce([3], '') as [3],
         coalesce([4], '') as [4],
         coalesce([5], '') as [5],
         coalesce([6], '') as [6],
         coalesce([7], '') as [7],
         coalesce([8], '') as [8],
         coalesce([9], '') as [9]
    FROM Divide  
   PIVOT (MAX(Match) FOR Col IN ([1], [2], [3], [4], [5], [6], [7], [8], [9])) AS PVT
ORDER BY Row ASC;