2

I have a postgresql table

alok=# select * from people;
  name   | country |  city   
---------+---------+---------
 alok    | india   | delhi
 ram     | india   | kolkata
 shyam   | india   | kanpur
 krishna | us      | newyork
 ajit    | india   | delhi
 sami    | india   | delhi
 mohamad | india   | kolkata
(7 rows)

which can be produced by

create table people(name varchar, country varchar, city varchar);

insert into people values('alok', 'india', 'delhi');
insert into people values('ram', 'india', 'kolkata');
insert into people values('shyam', 'india', 'kanpur');
insert into people values('krishna', 'us', 'newyork');
insert into people values('ajit', 'india', 'delhi');
insert into people values('sami', 'india', 'delhi');
insert into people values('mohamad', 'india', 'kolkata');

I want to produce aggregated name on country and city like

country |                name                
---------+------------------------------------
 india   | {"delhi":["alok", "ajit", "sami"], "kolkata":["ram", "mohamad"], "kanpur":["shyam"]}
 us      | {"newyork":["krishna"]}

2 Answers 2

3

demo:db<>fiddle

SELECT 
    country,
    json_object_agg(city, agg_names)
FROM (
    SELECT
        country,
        city, 
        json_agg(name) AS agg_names
    FROM
        people
    GROUP BY country, city
) s
GROUP BY country
  1. Group the names by city. To put the names into one JSON array you can use the aggregate function json_agg()
  2. Group this result by the country. To get the expected output you need to use the aggregate function json_object_agg() which creates key/pair values from city and name array

documentation

Sign up to request clarification or add additional context in comments.

Comments

0
select 
   country, json_agg( json_build_object( 'city', city, 'name', name  ) ) 
from (
   select country, city, json_agg( name ) as name
   from people
   group by country, city 
) a 
group by a.country;

2 Comments

Do not post just code. You should also add some explanation to your answer.
Your code is wrong. You are creating an array finally. This is not what the TO expected. See difference: dbfiddle.uk/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.