Query to create some groups based on activity

These look suspiciously like existing Trust Level groups (and the population of those is automated by similar measures) - why don’t you just amend the existing thresholds and have it all done for you?

/admin/site_settings/category/trust

e.g. for TL2 (members are in trust_level_2 or equivalent in your dialect):