Postgresql

时间:2019-03-06 11:01:58

标签: json postgresql

我有一个带有一个名为“ food”的json列的Postgresql表。

以下是一些行的示例:

food
["cheese", "salmon", "eggs"]
["salmon", "cheese", "eggs"]
["broccoli", "ham", "milk"]
["salmon", "cheese", "eggs", "pizza"]

当前结果:

food                                       count
["cheese", "salmon", "eggs"]              | 1
["salmon", "cheese", "eggs"]              | 1
["broccoli", "ham", "milk"]               | 1
["salmon", "cheese", "eggs", "pizza"]     | 1

所需结果:

food                                       count
["cheese", "salmon", "eggs"]              | 2
["broccoli", "ham", "milk"]               | 1
["salmon", "cheese", "eggs", "pizza"]     | 1

是否有一种方法可以不考虑元素的顺序来对JSON字段的内容进行GROUP BY?如果两行具有相同的确切内容,那么我希望将它们分组在一起。

我的计划是GROUP BY json_array_elements(food),但是由于某种原因,它仅返回每行的第一个元素。

2 个答案:

答案 0 :(得分:1)

实际上类似于@Scoots的答案,但没有任何排序,windows和aso:

SELECT (
    SELECT jsonb_agg(items order by items)
    FROM jsonb_array_elements(food) AS items
    ) AS food,
    count(*)
FROM test_json_grouping
GROUP BY 1;

...解释:

                                              QUERY PLAN                                              
------------------------------------------------------------------------------------------------------
 HashAggregate  (cost=1635.60..1890.60 rows=200 width=40)
   Group Key: (SubPlan 1)
   ->  Seq Scan on test_json_grouping  (cost=0.00..1629.25 rows=1270 width=32)
         SubPlan 1
           ->  Aggregate  (cost=1.25..1.26 rows=1 width=32)
                 ->  Function Scan on jsonb_array_elements items  (cost=0.00..1.00 rows=100 width=32)
(6 rows)

结果:

                 food                  | count 
---------------------------------------+-------
 ["cheese", "eggs", "salmon"]          |     2
 ["broccoli", "ham", "milk"]           |     1
 ["cheese", "eggs", "pizza", "salmon"] |     1
(3 rows)

答案 1 :(得分:0)

不直接-对于Postgres,它们是不同的字符串。

但是,我们可以做的是通过json_array_elements解压缩这些json字符串,然后用我们自己的json_agg排序将其重新打包。然后根据您的小组将他们同质化。

以下是一个查询,准确说明了我的意思:

select
    __food.food::text,
    count(1)
from (
    select
        json_agg(_unpack.food order by _unpack.food::text asc) as food
    from (
        select
            row_number() over(),
            json_array_elements(food) as food
        from
            YOUR_SCHEMA.YOUR_FOOD_TABLE
    ) as _unpack
    group by
        _unpack.row_number
) as __food
group by
    __food.food::text