Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Returning result in Json and table type sql oralce

I have a table in which with repeating id.
The goal is (to make a select) to get all the results of the repeating id to pour against each id in Json format as id it’s not in Json

id,yyyy,name 
1,2010,a  
1,2011,b
2,2010,a
3,2010,c
3,2011,a
4,2011,v

desired result

  id, column(json) 
  1     {"2010":"a","2011","b"}
  2     {"2010":"a","2010":"c"}
  3     {"2010":"c","2011":"a"}
  4     {"2011":"v"}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

with data(id, y, name) as (
    select 1,2010,'a' from dual union all  
    select 1,2011,'b' from dual union all
    select 2,2010,'a' from dual union all
    select 3,2010,'c' from dual union all
    select 3,2011,'a' from dual union all
    select 4,2011,'v' from dual -- union all
)
select id, json_objectagg(
    key to_char(y) value name) as obj
from data 
group by id
;

1   {"2010":"a","2011":"b"}
2   {"2010":"a"}
3   {"2010":"c","2011":"a"}
4   {"2011":"v"}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading