从 postgresql table 检索嵌套的 json 信息 - 没有当前索引

Retrieving nested json information from a postgresql table - no current index

我想从 postgresql table (prices) 的 jsonb 字段 (sub_table) 中检索嵌套的 json 信息。

我可以使用以下命令检索 json:

with jsontable as (
    SELECT "sub_table"
    FROM "prices"
    WHERE "Scenario" = 'A' AND "data_type" = 'new'
    )
SELECT * from jsontable 

这个 return 是一个 json table 像这样:

{
    "0": {
        "Name": "CompX",
        "Price": 10,
        "index": 1,
        "Date": "2020-01-09T00:00:00.000Z"
    },
    "1": {
        "Name": "CompY",
        "Price": 20,
        "index": 1,
        "Date": "2020-01-09T00:00:00.000Z"
    },
    "2": {
        "Name": "CompX",
        "Price": 19,
        "index": 2,
        "Date": "2020-01-10T00:00:00.000Z"
    }
}

我想要 return 所有与 Name = "CompX" 相关的数据,但无法使查询正常工作。

我已尝试遵循 examples here,但无法解决。我需要以某种方式重建索引吗?

您可以 return 提取 JSONB 数据 row-wisely 通过使用 jsonb_each 函数并按 (j.value -> 'Name')::text = '"CompX"' 条件过滤:

SELECT j.value
  FROM prices p
 CROSS JOIN jsonb_each(sub_table) AS j(e)
 WHERE (j.value -> 'Name')::text = '"CompX"'

Demo