Query gscopes uses a funnelback query to scope the results.
For a DB collection you'll presumably be mapping the DB fields returned by the SQL query to Funnelback meta classes so as long as you can come up with a Funnelback query that returns the set of results you want to group together you can use this as the query gscopes constraint.