TSQL Challenge 15 - Solution by Regan Wick



-- File Name: regan_wick_tsqlchallenge_15.sql
SELECT
       Row AS Col -- specified output appears to have this label refer to pivoted values
      ,[1],[2],[3],[4],[5],[6],[7],[8],[9]
FROM
      (
      --cross join Rows and Cols and build a xMarker colum where Row MOD Col = 0
      SELECT 
             CASE Rows.Row % Cols.Col
                  WHEN 0 THEN 'x'
                  ELSE ''
             END  AS xMarker
            ,Row
            ,Col
      FROM
            @Cols Cols
            CROSS JOIN @Rows Rows
      ) AS SourceData
--Pivot the Col values, use MIN because a function is required for Pivot
PIVOT
      (
      MIN(xMarker)
      FOR Col IN ([1],[2],[3],[4],[5],[6],[7],[8],[9])
      ) AS PivotData
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.