Cosmos DB - SQL - 获取所有唯一数组项和计数
Cosmos DB - SQL - Get All Unique Array Items and a count
我有一个由
等项组成的 cosmos 数据库
{
"id": 1
"countries": [
"Australia",
"New Zealand"
]
},
{
"id": 2
"countries": [
"Australia",
"Antarctica"
]
}
我知道我可以通过
获得不同的值结果
SELECT DISTINCT country FROM country IN t.countries
哪些returns数据像
{
"country": "Australia"
},
{
"country": "New Zealand"
},
{
"country": "Antarctica"
}
或
SELECT DISTINCT VALUE country FROM country IN t.countries
其中 returns 只是一个像
这样的数组
['Australia', 'New Zealand', 'Antarctica']
我将如何创建查询来获取数据,例如
{
"country": "Australia",
"count": 2
},
{
"country": "New Zealand",
"count": 1
},
{
"country": "Antarctica",
"count": 1
}
你可以用 GROUP BY 写这样的东西,
SELECT
COUNT(r) as Total,r as Country
FROM c JOIN r IN c.countries
GROUP BY r
结果,
[
{
"Total": 1,
"Country": "Antarctica"
},
{
"Total": 1,
"Country": "New Zealand"
},
{
"Total": 2,
"Country": "Australia"
} ]
我有一个由
等项组成的 cosmos 数据库{
"id": 1
"countries": [
"Australia",
"New Zealand"
]
},
{
"id": 2
"countries": [
"Australia",
"Antarctica"
]
}
我知道我可以通过
获得不同的值结果SELECT DISTINCT country FROM country IN t.countries
哪些returns数据像
{
"country": "Australia"
},
{
"country": "New Zealand"
},
{
"country": "Antarctica"
}
或
SELECT DISTINCT VALUE country FROM country IN t.countries
其中 returns 只是一个像
这样的数组['Australia', 'New Zealand', 'Antarctica']
我将如何创建查询来获取数据,例如
{
"country": "Australia",
"count": 2
},
{
"country": "New Zealand",
"count": 1
},
{
"country": "Antarctica",
"count": 1
}
你可以用 GROUP BY 写这样的东西,
SELECT
COUNT(r) as Total,r as Country
FROM c JOIN r IN c.countries
GROUP BY r
结果,
[ { "Total": 1, "Country": "Antarctica" }, { "Total": 1, "Country": "New Zealand" }, { "Total": 2, "Country": "Australia" } ]