Postgres GROUP BY on jsonb inner field

后端 未结 2 1247
终归单人心
终归单人心 2021-01-04 05:23

I am using Postgresql 9.4 and have a table test, with id::int and content::jsonb, as follows:



        
2条回答
  •  误落风尘
    2021-01-04 05:51

    You have to use the #>> operator instead of ->> when the right operand is a json path. Try this:

    SELECT json_agg(content) as content FROM test GROUP BY content #>> '{a,b}';
    

    Yields:

                  content
    ------------------------------------
     [{"a": {"c": 1}}]
     [{"a": {"b": 2}}]
     [{"a": {"b": 1}}, {"a": {"b": 1}}]
    (3 rows)
    

提交回复
热议问题