HTML tables take care of alignment. Why do you need padding? Anyhow, you could check for empty with
<if $fieldname eq ''>handle empty<else>$fieldname</if>
Thanks Mark, that got me thinking enough to find a solution, which I'll post below. There is probably something more elegant than this -- consider this a first shot.
The key to it is the select distincts to set up the rows and columns. I don't like the inefficiency of the repeated selects within two layers of loops. But maybe if you pre-select your results into a ram table and run this crosstab from that, it wouldn't be so expensive to run?
<sql "select distinct Rowfield rows from table order by rows"></sql>
<sql "select distinct Colfield cols from table order by cols"></sql>