我有格式的文件:
{
"firstName": "Emery",
"lastName": "Wing",
"gender" : "F"
"userId": "00Z",
"peers": [
{
"peerId": "42F"
},
{
"peerId": "14A"
}
]
}
,
{
"firstName": "Brian",
"lastName": "Wing",
"gender" : "M"
"userId": "42F",
"peers": [
{
"peerId": "00Z"
},
{
"peerId": "5VH"
}
]
},
{
"firstName": "Kevin",
"lastName": "Hart",
"userId": "22N",
"gender": "M"
},
.......
我想选择姓氏为“Wing”的所有用户及其对等方的详细信息。 如您所见,'peers'集合存储了他的同行文档的元ID。
For this我试图将userId和PeerId存储到单个数组中,然后使用连接来获取所有这些信息,但由于两个字段都处于不同的级别,因此找不到这样做的方法。
我使用的查询和结果如下:
Select ARRAY_AGG([u.userId,u.peers[*].peerId]) as ID
From user u
Where u.type = 'User'
and u.lastName = 'Wing'
;
我期待它创建一个单个数组,所有值都在同一级别上,但我得到了以下结果:
"ID": [
[
"00Z",
[
"42F",
"14A"
]
],
[
"42F",
[
"00Z",
"5VH"
]
]
]
}
我想要的结果是所有这些不同的ID:
"ID": [
"00Z",
"42F",
"14A"
"5VH"
]
这样我就可以申请加入并获取这些不同ID的详细信息。
请帮助..
答案 0 :(得分:0)
Select ARRAY_DISTINCT(ARRAY_FLATTEN(ARRAY_AGG([u.userId,u.peers[*].peerId]),3)) as ID
From user u
Where u.type = 'User'
and u.lastName = 'Wing'
;
OR
SELECT
ARRAY_DISTINCT(ARRAY_FLATTEN(ARRAY_AGG(ARRAY_INSERT(IFMISSINGORNULL(u.peers[*].peerId,[]),0,u.userId)),2)) AS ID
FROM user AS u
WHERE u.type = 'User' AND u.lastName = 'Wing';
答案 1 :(得分:0)
此查询有效:
Select ARRAY_DISTINCT(ARRAY_FLATTEN(ARRAY_AGG(ARRAY_INSERT(IFMISSINGORNULL(u.peers[*].peerId,[]),0,u.userId)),1)) As ID
From user u
Where u.type = 'User'
and u.lastName = 'Wing'