【发布时间】:2021-10-24 15:27:09
【问题描述】:
使用 PostgreSQL 12.7,我想从嵌套的 JSON 数组中获取产品的最新版本(最大值)。这是fields 列中product 'AAA' 的示例值:
"customfield_01":[
{
"id":1303,
"name":"AAA - 1.82.0",
"state":"closed",
"boardId":137,
"endDate":"2021-10-15T10:00:00.000Z",
"startDate":"2021-10-04T01:00:01.495Z",
"completeDate":"2021-10-18T03:02:55.824Z"
},
{
"id":1304,
"name":"AAA - 1.83.0",
"state":"active",
"boardId":137,
"endDate":"2021-10-29T10:00:00.000Z",
"startDate":"2021-10-18T01:00:24.324Z"
}
],
我试过了:
SELECT product, jsonb_path_query_array(fields, '$.customfield_01.version') AS version
FROM product.issues;
这是输出:
| product | version |
|---------------------------------------------------------|
| CCC |[] |
| AAA |["AAA - 1.83.0", "AAA - 1.82.0"] |
| BBB |["BBB - 1.83.0", "BBB - 1.82.0", "BBB - 1.84.0]|
| BBB |["BBB - 1.83.0"] |
| BBB |["BBB - 1.84.0", "BBB - 1.83.0"] |
预期是:
| product | version |
|---------------------------------------------------------|
| AAA |["AAA - 1.83.0" |
| BBB |["BBB - 1.84.0] |
| BBB |["BBB - 1.83.0"] |
| BBB |["BBB - 1.84.0"] |
尝试了 unnest/Array 但它抛出了一个错误:
SELECT max(version)
FROM (SELECT UNNEST(ARRAY [jsonb_path_query_array(fields,'$.customfield_01.version')]) AS version FROM product.issues ) AS version;
确实使用了-1,但它只会获取最右边的数据。
jsonb_path_query_array(fields, '$.customfield_01.version') ->> -1
Postgres 和 json 非常新。确实尝试阅读文档和谷歌,但多次尝试失败。
【问题讨论】:
-
请提供您的 input 值,并始终提供您的 Postgres 版本。
-
谢谢。刚刚用输入值更新了帖子。
-
Select version();? -
正在研究如何获得它,谢谢!这是 PostgreSQL 12.7 的版本
标签: postgresql jsonb json-path-expression