简体   繁体   中英

Select all the rows with same id and same value in another column without using having clause

I have cosmos DB with a container having multiple documents. I want to get all the ids with the same value of a property. Since it's Cosmos I cannot use the having clause.

eg: If there is a container with the schema,

{
 "id": 1,
 "source": "online",
 "type": "login"
},
{
 "id": 1,
 "source": "online",
 "type": "login"
},
{
 "id": 2,
 "source": "online",
 "type": "login"
},
{
 "id": 2,
 "source": "In store",
 "type": "login"
}

I want all the ids where the source value is all same and "online". So in the above example, it should return "id" as 1 only.

This request selects all IDs that did not have other source types at all, only those online

 select distinct(id), source
    from(
    select id, source
    from your_table as t
    WHERE NOT EXISTS (SELECT id
                                   FROM your_table 
                                   WHERE source != 'online'
                                   AND   your_table.id = t.id))x

-- If need at the end use this WHERE x.id IS NOT NULL


Result:

| id   | source 
|:---- |:------:
| 1    | online  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM