Fixing SQL bug.  Using column namen in select part cannot be used in the where part