Postgres json重点计数

时间:2014-01-08 14:17:50

标签: json postgresql

给定包含json列的N条记录

|ID    |Name   |JSON                             
|01    |TEST1  |{"key1" : "value1", "key2": "value2", "key4": "value4"}
|02    |TEST1  |{"key1" : "value1"}
|03    |TEST2  |{"key1" : "value1", "key2": "value2", "key3":"value3"}
...

计算一组键的每个json值的出现的最佳策略是什么,对于上面的示例,我将限制为key1,key2,key3并获取: -

|value1|value2|value3|
|3     |2     |1     |

值会发生变化,所以我真的不想明确地查找它们。

1 个答案:

答案 0 :(得分:3)

CREATE TABLE test (id INT4 PRIMARY KEY, some_name TEXT, j json);
copy test FROM stdin;
01  TEST1   {"key1" : "value1", "key2": "value2", "key4": "value4"}
02  TEST1   {"key1" : "value1"}
03  TEST2   {"key1" : "value1", "key2": "value2", "key3":"value3"}
\.
with unpacked as (
    SELECT (json_each_text(j)).* FROM test
)
SELECT value, count(*) FROM unpacked WHERE key in ('key1', 'key2', 'key3') group by value;

返回:

 value  | count 
--------+-------
 value1 |     3
 value3 |     1
 value2 |     2
(3 rows)

像你所展示的那样回归它并没有让我觉得好主意(如果有40亿个不同的值,你想做什么?),但是你总是可以在你的应用中转动,或者修改查询来做转动。