【发布时间】:2021-05-13 13:51:47
【问题描述】:
我有如下所示的示例数据:
表:
create table tbl_jdata
(
id int,
jdata json
);
insert into tbl_jdata values(1,'[{"salary":10000,"name":"mak"},{"salary":20000,"name":"jak"},{"salary":45000,"name":"abc"}]');
我只想显示一个薪水最高的 json 元素,例如预期结果中所示。
预期结果:
id jdata
-------------------------------------
1 [{"salary":45000,"name":"abc"}]
我的尝试:
select t.id,each_section
from tbl_jdata t
cross join unnest(t.jdata) each_section
where t.id = 1 and (each_section ->> 'salary') in
(
select max(each_section ->> 'salary')
from tbl_jdata t
cross join unnest(t.jdata) each_section
);
得到一个错误:
ERROR: function unnest(json) does not exist
【问题讨论】:
-
@a_horse_with_no_name,抱歉,
jdata列的实际数据类型是json,并且数据已以给定格式存储。