TSQL Challenge 15 - Solution by Dave Ballantyne



-- File Name: dave_ballantyne_tsqlchallenge_15.sql
with cteCalcs(Row,Col,Marker)
as
(
select rows.row,1,'x'
  from @Rows rows 
)
select Row as col ,
       'x' as '1',
       case Row&1 when 0 then 'x' else ' ' end as '2',
       case Row%3 when 0 then 'x' else ' ' end as '3',
       case when Row&1=0 and Row%4=0 then 'x' else ' ' end as '4',
       case Row%5 when 0 then 'x' else ' ' end as '5',
       case when Row&1=0 and Row%6=0 then 'x' else ' ' end as '6',
       case Row%7 when 0 then 'x' else ' ' end as '7',
       case when Row&1=0 and Row%8=0 then 'x' else ' ' end as '8',
       case Row%9 when 0 then 'x' else ' ' end as '9'
  from ctecalcs
  pivot(
          min(Marker)
          for Col in([1])
     ) as pvt
  order by row;