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?










share|improve this question
























  • select version()?
    – Clodoaldo Neto
    Apr 17 '17 at 15:28















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?










share|improve this question
























  • select version()?
    – Clodoaldo Neto
    Apr 17 '17 at 15:28













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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)





share|improve this answer





















  • Thank you. This is what I was looking for!
    – The Kingdom Tech
    Apr 17 '17 at 16:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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)





share|improve this answer





















  • Thank you. This is what I was looking for!
    – The Kingdom Tech
    Apr 17 '17 at 16:27















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)





share|improve this answer





















  • Thank you. This is what I was looking for!
    – The Kingdom Tech
    Apr 17 '17 at 16:27













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)





share|improve this answer












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)






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

How to send String Array data to Server using php in android

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Is anime1.com a legal site for watching anime?