switched calculation to sql
don't need to get all the messages do we?
distinct -- will maybe speed up sql