Postgres GROUP BY on jsonb inner field

后端 未结 2 1223
终归单人心
终归单人心 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 06:06

    I think json_agg() is not the best choice to use it here, since that is concatenating the content values (the whole json data) into an array for a specific group.
    It makes more sense to use something like this (and I added 'count(*)', just to have a more common scenario):

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

提交回复
热议问题