If you're just trying to extract the data per usergroup then you don't even need the:
PHP Code:
AND membergroupids=13
part of the query, because you're trying to find based on the usergroup not other membergroupids.
For instance if you're trying to find the sum for the Administrators usergroup the query will fail even if you add MySQL syntax to search the data of membergroupsid because it won't find the membergroupid of 6. If you're trying to incorporate membergroupids field then you should use 'OR' instead of 'AND'.
Hopefully that helps somewhat.
Cheers,
g-force2k2