'Cosmos SQL query with group by subquery

I have a collection of documents similar to the below. The sub property is supposed to be unique across the collection, but introduced bugs violated this. I'm trying to find any offending documents where the same sub property is shared, ideally in a single query (I can get the results in a pair of separate queries, but its not ideal)

I've tried various forms of join / where exists, but it produces an empty result set.

{
    "id": "some_guid",
    "sub": "some_guid"
}

-- Attempt

SELECT c.id,c.sub FROM c
where (select value grouping.sub from (
       SELECT c.sub,count(1) as cnt FROM c
       group by c.sub) as grouping
     where grouping.cnt > 1) = c.sub


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source