charts fixed for postgres migration

… and move the SQL out of the view :-)