Yeah you need the group by.
My bad though, the group by clause comes after the where clause, not before; I pasted the copy into the wrong place. Switch them around and it should work. 'type' is not required within the select, but helps to keep track of the data you need.
header('HTTP/1.1 420 Enhance Your Calm');