我有此数据:
invid | contents
-------+---------------------------------
109 | {"49": {"22000.00": "10.00"}}
108 | {"49": {"22000.00": "312.00"}}
106 | {"48": {"50000.00": "20.00"}}
103 | {"50": {"1020000.00": "20.00"}}
102 | {"48": {"50500.00": "20.00"}}
101 | {"52": {"30060000.00": "0.00"}}
89 | {"50": {"1020000.00": "2.00"}}
88 | {"48": {"50000.00": "20.00"}}
87 | {"50": {"1020000.00": "1.00"}}
86 | {"50": {"10020000.00": "3.00"}}
83 | {"48": {"50000.00": "30.00"}}
82 | {"50": {"1020000.00": "4.00"}}
81 | {"47": {"20000.00": "40.00"}}
80 | {"48": {"50000.00": "200.00"}}
79 | {"50": {"1020000.00": "20.00"}}
我想要结果:
[{50:5,48:4,49:3,52:1,47:1}]
或
[{'50':5,'48':4,'49':3,'52':1,'47':1}]
降序。
答案 0 :(得分:0)
您需要提取所有键并将结果分组:
select ky, count(*) as numkeys
from data
cross join lateral jsonb_object_keys(contents) as t(ky)
group by ky
order by ky;
如果您想要一个带有结果的JSON数组,则可以使用jsonb_object_agg()
:
select jsonb_object_agg(ky, numkeys)
from (
select ky, count(*) as numkeys
from data
cross join lateral jsonb_object_keys(contents) as t(ky)
group by ky
order by ky
) t;