Some changes to db queries to help speed things up
query juggling to improve speed
remove (my) duplicated line
corrected query
Merge pull request #982 from nuthead/dev

sql query improvements