json_agg Two columns in Postgres
up vote
2
down vote
favorite
I have a table which has the following contains two columns that look like the following:
rev | level
-----------
B | 1001
B | 1002
B | 1003
C | 1004
C | 1005
D | 1006
I am trying to return a column that looks like this:
{"B":["1001","1002","1003"], "C":["1002","1003"], "D":["1006"]}
the best I could get was using this query:
SELECT d.rev,
to_json(ARRAY(SELECT level
FROM details
WHERE rev = d.rev
GROUP BY level
ORDER BY level DESC
)) AS level
FROM details d
GROUP BY d.rev
ORDER BY d.rev DESC
This gives me the following rows :
____________________________________
| B | ["1001","1002","1003"] |
| C | ["1004","1005"] |
| D | ["1006"] |
|__________________________________|
How do I combine these columns into one JSON object?
arrays json postgresql aggregate-functions
add a comment |
up vote
2
down vote
favorite
I have a table which has the following contains two columns that look like the following:
rev | level
-----------
B | 1001
B | 1002
B | 1003
C | 1004
C | 1005
D | 1006
I am trying to return a column that looks like this:
{"B":["1001","1002","1003"], "C":["1002","1003"], "D":["1006"]}
the best I could get was using this query:
SELECT d.rev,
to_json(ARRAY(SELECT level
FROM details
WHERE rev = d.rev
GROUP BY level
ORDER BY level DESC
)) AS level
FROM details d
GROUP BY d.rev
ORDER BY d.rev DESC
This gives me the following rows :
____________________________________
| B | ["1001","1002","1003"] |
| C | ["1004","1005"] |
| D | ["1006"] |
|__________________________________|
How do I combine these columns into one JSON object?
arrays json postgresql aggregate-functions
select version()?
– Clodoaldo Neto
Apr 17 '17 at 15:28
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a table which has the following contains two columns that look like the following:
rev | level
-----------
B | 1001
B | 1002
B | 1003
C | 1004
C | 1005
D | 1006
I am trying to return a column that looks like this:
{"B":["1001","1002","1003"], "C":["1002","1003"], "D":["1006"]}
the best I could get was using this query:
SELECT d.rev,
to_json(ARRAY(SELECT level
FROM details
WHERE rev = d.rev
GROUP BY level
ORDER BY level DESC
)) AS level
FROM details d
GROUP BY d.rev
ORDER BY d.rev DESC
This gives me the following rows :
____________________________________
| B | ["1001","1002","1003"] |
| C | ["1004","1005"] |
| D | ["1006"] |
|__________________________________|
How do I combine these columns into one JSON object?
arrays json postgresql aggregate-functions
I have a table which has the following contains two columns that look like the following:
rev | level
-----------
B | 1001
B | 1002
B | 1003
C | 1004
C | 1005
D | 1006
I am trying to return a column that looks like this:
{"B":["1001","1002","1003"], "C":["1002","1003"], "D":["1006"]}
the best I could get was using this query:
SELECT d.rev,
to_json(ARRAY(SELECT level
FROM details
WHERE rev = d.rev
GROUP BY level
ORDER BY level DESC
)) AS level
FROM details d
GROUP BY d.rev
ORDER BY d.rev DESC
This gives me the following rows :
____________________________________
| B | ["1001","1002","1003"] |
| C | ["1004","1005"] |
| D | ["1006"] |
|__________________________________|
How do I combine these columns into one JSON object?
arrays json postgresql aggregate-functions
arrays json postgresql aggregate-functions
edited Nov 13 at 3:28
klin
53.6k54573
53.6k54573
asked Apr 17 '17 at 14:44
The Kingdom Tech
155
155
select version()?
– Clodoaldo Neto
Apr 17 '17 at 15:28
add a comment |
select version()?
– Clodoaldo Neto
Apr 17 '17 at 15:28
select version()?– Clodoaldo Neto
Apr 17 '17 at 15:28
select version()?– Clodoaldo Neto
Apr 17 '17 at 15:28
add a comment |
1 Answer
1
active
oldest
votes
up vote
6
down vote
accepted
You can use json_agg() in your query:
select rev, json_agg(level) levels
from details
group by 1
order by 1;
rev | levels
-----+--------------------
B | [1001, 1002, 1003]
C | [1004, 1005]
D | [1006]
(3 rows)
and json_object_agg() to aggregate the result to a single json:
select json_object_agg(rev, levels order by rev)
from (
select rev, json_agg(level) levels
from details
group by 1
) s;
json_object_agg
----------------------------------------------------------------
{ "B" : [1001, 1002, 1003], "C" : [1004, 1005], "D" : [1006] }
(1 row)
Thank you. This is what I was looking for!
– The Kingdom Tech
Apr 17 '17 at 16:27
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
You can use json_agg() in your query:
select rev, json_agg(level) levels
from details
group by 1
order by 1;
rev | levels
-----+--------------------
B | [1001, 1002, 1003]
C | [1004, 1005]
D | [1006]
(3 rows)
and json_object_agg() to aggregate the result to a single json:
select json_object_agg(rev, levels order by rev)
from (
select rev, json_agg(level) levels
from details
group by 1
) s;
json_object_agg
----------------------------------------------------------------
{ "B" : [1001, 1002, 1003], "C" : [1004, 1005], "D" : [1006] }
(1 row)
Thank you. This is what I was looking for!
– The Kingdom Tech
Apr 17 '17 at 16:27
add a comment |
up vote
6
down vote
accepted
You can use json_agg() in your query:
select rev, json_agg(level) levels
from details
group by 1
order by 1;
rev | levels
-----+--------------------
B | [1001, 1002, 1003]
C | [1004, 1005]
D | [1006]
(3 rows)
and json_object_agg() to aggregate the result to a single json:
select json_object_agg(rev, levels order by rev)
from (
select rev, json_agg(level) levels
from details
group by 1
) s;
json_object_agg
----------------------------------------------------------------
{ "B" : [1001, 1002, 1003], "C" : [1004, 1005], "D" : [1006] }
(1 row)
Thank you. This is what I was looking for!
– The Kingdom Tech
Apr 17 '17 at 16:27
add a comment |
up vote
6
down vote
accepted
up vote
6
down vote
accepted
You can use json_agg() in your query:
select rev, json_agg(level) levels
from details
group by 1
order by 1;
rev | levels
-----+--------------------
B | [1001, 1002, 1003]
C | [1004, 1005]
D | [1006]
(3 rows)
and json_object_agg() to aggregate the result to a single json:
select json_object_agg(rev, levels order by rev)
from (
select rev, json_agg(level) levels
from details
group by 1
) s;
json_object_agg
----------------------------------------------------------------
{ "B" : [1001, 1002, 1003], "C" : [1004, 1005], "D" : [1006] }
(1 row)
You can use json_agg() in your query:
select rev, json_agg(level) levels
from details
group by 1
order by 1;
rev | levels
-----+--------------------
B | [1001, 1002, 1003]
C | [1004, 1005]
D | [1006]
(3 rows)
and json_object_agg() to aggregate the result to a single json:
select json_object_agg(rev, levels order by rev)
from (
select rev, json_agg(level) levels
from details
group by 1
) s;
json_object_agg
----------------------------------------------------------------
{ "B" : [1001, 1002, 1003], "C" : [1004, 1005], "D" : [1006] }
(1 row)
answered Apr 17 '17 at 15:31
klin
53.6k54573
53.6k54573
Thank you. This is what I was looking for!
– The Kingdom Tech
Apr 17 '17 at 16:27
add a comment |
Thank you. This is what I was looking for!
– The Kingdom Tech
Apr 17 '17 at 16:27
Thank you. This is what I was looking for!
– The Kingdom Tech
Apr 17 '17 at 16:27
Thank you. This is what I was looking for!
– The Kingdom Tech
Apr 17 '17 at 16:27
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43453685%2fjson-agg-two-columns-in-postgres%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
select version()?– Clodoaldo Neto
Apr 17 '17 at 15:28