• This seems to be more of an HTML/CSS formatting question than a SQL Server question, so perhaps try this:

    1. Take the HTML created by the code you posted and output it with: select @tableHTML

    2. Paste that output into Notepad and view it in a web browser.

    3. Manually edit it by trial and error it until you get the desired result using references such as the "HTML Style Example - Font, Color and Size" example at w3schools.

    4. Put the formatting want back into the T-SQL.

    For example, this code makes the font of the red cells larger and they are sorted/ORDERed BY the number of failed records. (Is sorting what you meant by controlling the position of tr?)

    SELECT td = Date, '', td = WebServerName, '', td = Location, '', td = cnt_RecsTotal, '',

    'td/@style' = CASE WHEN cnt_RecsFailed > 0 THEN 'background-color:#FF3366; font-size:20pt;' END,

    td = cnt_RecsFailed

    FROM #tmpDailyReport

    ORDER BY cnt_RecsFailed

    for xml path('tr'), TYPE

    ) AS NVARCHAR(MAX) ) +

    N'</table>' +

    N'

    ' ;