【问题标题】:Save output of jsonb_each in a materialized view在物化视图中保存 jsonb_each 的输出
【发布时间】:2021-04-26 16:02:06
【问题描述】:

我在 PG 12 中有一个表“t”-

Id int

Col1 character varying

Data_col jsonb

Data_col 如下所示:

{key1:value 1,key2:value 2,key3:value 3....}

我可以使用下面的选择以拖曳形式获取键和值

Select t.id, t.col1, x.key, x.value
From t, jsonb_each_text(t.data_col) x

我想知道是否有办法将上述选择的输出存储到物化视图中? 我试过了-

Create materialized view t_mv
As
Select t.id, t.col1, x.key, x.value
From t, jsonb_each_text(t.data_col) x;

但我得到一个错误

error cannot call jsonb_each on a non-object

有什么想法吗?

谢谢

【问题讨论】:

    标签: json postgresql


    【解决方案1】:

    t.data_col 中的一个值必须是数组或标量:

    SELECT * FROM jsonb_each_text('"scalar"'::jsonb);
    ERROR:  cannot call jsonb_each_text on a non-object
    
    SELECT * FROM jsonb_each_text('[1,2,3]'::jsonb);
    ERROR:  cannot call jsonb_each_text on a non-object
    

    要查找有问题的行,您可以使用类似的查询

    SELECT id FROM t WHERE jsonb_typeof(data_col) <> 'object';
    

    【讨论】:

      【解决方案2】:

      您的尝试应该会奏效。但是,您的查询中的列名不匹配。查询使用 col2,但表是使用 col1 定义的。

      【讨论】:

      • 感谢您的指出。这是一个错字,我已经更正了。
      猜你喜欢
      • 2021-07-16
      • 2019-12-25
      • 1970-01-01
      • 2020-03-02
      • 2013-06-15
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      • 2019-10-07
      相关资源
      最近更新 更多