TSQL Challenge 15 - Solution by Sean Blackburne



-- File Name: Sean_Blackburne_tsqlchallenge_15.sql
select 
   RowVal as Row,
   case [1] when 1 then 'X' else '' end as [1],
   case [2] when 1 then 'X' else '' end as [2],
   case [3] when 1 then 'X' else '' end as [3],
   case [4] when 1 then 'X' else '' end as [4],
   case [5] when 1 then 'X' else '' end as [5],
   case [6] when 1 then 'X' else '' end as [6],
   case [7] when 1 then 'X' else '' end as [7],
   case [8] when 1 then 'X' else '' end as [8],
   case [9] when 1 then 'X' else '' end as [9]
from 
(
   select
      RowVal,
      [1], 
      [2], 
      [3],
      [4],
      [5],
      [6],
      [7],
      [8],
      [9]
   from
   ( 
      select   
         Row,Row as RowVal,Col
      from @Rows r
      cross join @Cols c
      where Row % Col=0 
   ) p 
   pivot 
   (
      count(Row) for Col in ([1],[2],[3],[4],[5],[6],[7],[8],[9])
   ) AS pvt
) as x

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.