Dataview has some GROUP BY functionality. Otherwise, you might consider making separate queries that filter just the second-dimension tags one at a time.
Yes, that’s what I meant above. If you don’t want to use Dataview, you might have to make a few separate queries, rather than grouping them dynamically. (I don’t think it’s possible to group dynamically with queries. It might be.)