当有一个 ARRAY 也在另一个 ARRAY 中时,如何在不使用 UNNEST 的情况下使用 ARRAY 函数?
How to use ARRAY function without using UNNEST when there is an ARRAY which is also inside an another ARRAY?
下面是示例文档
[
{
"trans-data":
{
"$Docver":"1.0",
"ManufId":"1234543",
"ToyDot":
{
"GrossAmt":"675",
"Wqty":"200"
},
"Mflnitmlst":
[
{
"Mfprcdv":"25000",
"SaleDt1":
[
{
"Mtid":"0987655",
"ordrqty":"102",
"Plainqty":"1000"
}
]
},
{
"Mfprcdv":"25000",
"SaleDt1":
[
{
"Mtid":"0987656",
"ordrqty":"110",
"Plainqty":"1500"
}
]
},
{
"Mfprcdv":"25000",
"SaleDt1":
[
{
"Mtid":"0987657",
"ordrqty":"120",
"Plainqty":"2000"
}
]
}
],
"Tmstp":"2021-03-04T14:23:21",
"Sectn":"XYZ"
}
},
{
"trans-data":
{
"$Docver":"1.0",
"ManufId":"1234543",
"ToyDot":
{
"GrossAmt":"605",
"Wqty":"100"
},
"Mflnitmlst":
[
{
"Mfprcdv":"26000",
"SaleDt1":
[
{
"Mtid":"1987655",
"ordrqty":"102",
"Plainqty":"1000"
}
]
},
{
"Mfprcdv":"26000",
"SaleDt1":
[
{
"Mtid":"1987656",
"ordrqty":"110",
"Plainqty":"1500"
}
]
},
{
"Mfprcdv":"26000",
"SaleDt1":
[
{
"Mtid":"1987657",
"ordrqty":"120",
"Plainqty":"2000"
}
]
}
],
"Tmstp":"2021-03-04T14:23:21",
"Sectn":"XYZ"
}
}
]
我写了下面的 N1QL 查询并得到了下面的结果
查询:
SELECT DISTINCT ARRAY {V.ordrqty,V.Plainqty} FOR V IN M.SaleDt1 WHEN V.ordrqty IN [120] END
FROM `trans-data` AS T
UNNEST Mflnitmlst as M
WHERE t.Sectn="XYZ"
结果:
[{
"": []
},
{
"": [{
"ordrqty": 120,
"Plainqty": "2000"
}]
},
{}
]
现在想不使用UNNEST函数进行查询。有人可以帮忙吗?
根据您的条件构造新数组
SELECT ARRAY_DISTINCT(ARRAY_FLATTEN(ARRAY (ARRAY {s.ordrqty,s.Plainqty}
FOR s IN m.SaleDt1
WHEN s.ordrqty IN [120]
END)
FOR m IN t.Mflnitmlst
END, 1) ) AS orders
FROM `trans-data` AS t
WHERE t.Sectn="XYZ"
AND (ANY m IN t.Mflnitmlst
SATISFIES (ANY s IN m.SaleDt1
SATISFIES s.ordrqty IN [120]
END)
END);
下面是示例文档
[
{
"trans-data":
{
"$Docver":"1.0",
"ManufId":"1234543",
"ToyDot":
{
"GrossAmt":"675",
"Wqty":"200"
},
"Mflnitmlst":
[
{
"Mfprcdv":"25000",
"SaleDt1":
[
{
"Mtid":"0987655",
"ordrqty":"102",
"Plainqty":"1000"
}
]
},
{
"Mfprcdv":"25000",
"SaleDt1":
[
{
"Mtid":"0987656",
"ordrqty":"110",
"Plainqty":"1500"
}
]
},
{
"Mfprcdv":"25000",
"SaleDt1":
[
{
"Mtid":"0987657",
"ordrqty":"120",
"Plainqty":"2000"
}
]
}
],
"Tmstp":"2021-03-04T14:23:21",
"Sectn":"XYZ"
}
},
{
"trans-data":
{
"$Docver":"1.0",
"ManufId":"1234543",
"ToyDot":
{
"GrossAmt":"605",
"Wqty":"100"
},
"Mflnitmlst":
[
{
"Mfprcdv":"26000",
"SaleDt1":
[
{
"Mtid":"1987655",
"ordrqty":"102",
"Plainqty":"1000"
}
]
},
{
"Mfprcdv":"26000",
"SaleDt1":
[
{
"Mtid":"1987656",
"ordrqty":"110",
"Plainqty":"1500"
}
]
},
{
"Mfprcdv":"26000",
"SaleDt1":
[
{
"Mtid":"1987657",
"ordrqty":"120",
"Plainqty":"2000"
}
]
}
],
"Tmstp":"2021-03-04T14:23:21",
"Sectn":"XYZ"
}
}
]
我写了下面的 N1QL 查询并得到了下面的结果
查询:
SELECT DISTINCT ARRAY {V.ordrqty,V.Plainqty} FOR V IN M.SaleDt1 WHEN V.ordrqty IN [120] END
FROM `trans-data` AS T
UNNEST Mflnitmlst as M
WHERE t.Sectn="XYZ"
结果:
[{
"": []
},
{
"": [{
"ordrqty": 120,
"Plainqty": "2000"
}]
},
{}
]
现在想不使用UNNEST函数进行查询。有人可以帮忙吗?
根据您的条件构造新数组
SELECT ARRAY_DISTINCT(ARRAY_FLATTEN(ARRAY (ARRAY {s.ordrqty,s.Plainqty}
FOR s IN m.SaleDt1
WHEN s.ordrqty IN [120]
END)
FOR m IN t.Mflnitmlst
END, 1) ) AS orders
FROM `trans-data` AS t
WHERE t.Sectn="XYZ"
AND (ANY m IN t.Mflnitmlst
SATISFIES (ANY s IN m.SaleDt1
SATISFIES s.ordrqty IN [120]
END)
END);