mostly working, haven't figured out how tGROUP BY and ORDER BY with postgresql