-- File Name: bill_sheets_tsqlchallenge_15.sql with cteMods as ( select Row, Col, Rslt = case Row % Col when 0 then 'x' else '' end from @Rows cross join @Cols ) select Row, [1], [2], [3], [4], [5], [6], [7], [8], [9] from ( select Row, Col, Rslt from cteMods ) as p1 pivot ( min(Rslt) for Col in ([1], [2], [3], [4], [5], [6], [7], [8], [9]) ) as pvt order by Row;