0

Starting with the data table below, how can I get a return JSON object with the keys for each region with the sum of their occurrence as the value, nested within the region's country_code?

Example Table

+---------------------+------------------+
|    country_code     |      region      |
+---------------------+------------------+
|        'CA'         |     'Ontario'    |
+---------------------+------------------+
|        'CA'         |     'Ontario'    |
+---------------------+------------------+
|        'CA'         |     'Ontario'    |
+---------------------+------------------+
|        'CA'         |     'Quebec'     |
+---------------------+------------------+
|        'CA'         |     'Quebec'     |
+---------------------+------------------+
|        'DE'         |     'Bavaria'    |
+---------------------+------------------+
|        'DE'         |     'Bavaria'    |
+---------------------+------------------+
|        'DE'         |     'Bavaria'    |
+---------------------+------------------+
|        'DE'         |     'Bavaria'    |
+---------------------+------------------+
|        'DE'         |    'Saarland'    |
+---------------------+------------------+
|        'DE'         |     'Berlin'     |
+---------------------+------------------+
|        'DE'         |     'Berlin'     |
+---------------------+------------------+
|        'JP'         |     'Tokyo'      |
+---------------------+------------------+

Expected Result

[
  { 'CA': [
      { 'Ontario': 3 },
      { 'Quebec': 2 }
    ]
  },
  { 'DE': [
      { 'Bavaria': 4 },
      { 'Saarland': 1 },
      { 'Berlin': 2 }
    ]
  },
  { 'JP': [
      { 'Tokyo': 1 }
    ]
  }
]

1 Answer 1

1

Quick and dirty

with tbl(country_code,region) 
  as (values ('CA', 'Ontario')
  ,('CA', 'Ontario')
  ,('CA', 'Ontario')
  ,('CA', 'Quebec')
  ,('CA', 'Quebec')
  ,('DE', 'Bavaria')
  ,('DE', 'Bavaria')
  ,('DE', 'Bavaria')
  ,('DE', 'Saarland')
  ,('DE', 'Berlin')
  ,('DE', 'Berlin')
  ,('JP', 'Tokyo')
)
, groups as (select country_code,jsonb_build_object(region,count(*)) as x from tbl group by country_code,region)
, l1 as (select jsonb_build_object(country_code,jsonb_agg(x)) as y from groups group by country_code)
select jsonb_agg(y) from l1;

Returns:

jsonb_agg                                                                                                                   |
----------------------------------------------------------------------------------------------------------------------------|
[{"CA": [{"Quebec": 2}, {"Ontario": 3}]}, {"JP": [{"Tokyo": 1}]}, {"DE": [{"Saarland": 1}, {"Berlin": 2}, {"Bavaria": 3}]}] |
Sign up to request clarification or add additional context in comments.

Comments

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.