Open this example in the interactive library
Source code
WITH samples(t,site,n) AS (VALUES(8,'N',2),(8,'S',3),(9,'N',4)) SELECT t,json_group_object(site,n) AS sites FROM samples GROUP BY t ORDER BY t;
Sample output
[
{
"t": 8,
"sites": "{\"N\":2,\"S\":3}"
},
{
"t": 9,
"sites": "{\"N\":4}"
}
]