Grouping columns from a nested select











up vote
0
down vote

favorite












I have a table table1 with the following columns:



id (INT)
label (VARCHAR)
value (INT)


I want to retrieve both the average of the value column, the entire row where the value is the largest, and the row with the lowest value. Something like this:



{
average: xxx,
maxval: {
id: x,
label: x,
value: x
},
minval: {
id: x,
label: x,
value: x
}
}


Tried with the following query:



SELECT 
AVG(a.value) AS average,
b.*,
c.*
FROM table1 a
INNER JOIN table1 b ON ( b.value = (SELECT MAX(value) FROM table1 WHERE label = "el"))
INNER JOIN table1 c ON ( c.value = (SELECT MIN(value) FROM table1 WHERE label = "el"))
WHERE a.label = "el";


but this will return all the fields in a single level, like this:



{
average,
id,
label,
value,
id,
label,
value
}


Tried this too:



SELECT 
AVG(a.value) AS average,
(SELECT b.* FROM table1 b WHERE ( b.value = (SELECT MAX(value) FROM table1 WHERE label = "el"))) AS maxval,
(SELECT c.* FROM table1 c WHERE ( c.value = (SELECT MIN(value) FROM table1 WHERE label = "el"))) AS minval
FROM table1 a
WHERE a.label = "el";


This last one give an error as I'm trying to use a single alias on multiple columns.



Using MySQL 5.5.










share|improve this question


















  • 2




    You cant return a composite result unless you use JSON
    – Juan Carlos Oropeza
    Nov 19 at 20:17










  • How are you returning that output from SQL? Are you using application layer like PHP?
    – Parfait
    Nov 19 at 20:47










  • @Parfait sending query from nodejs
    – neptune
    Nov 19 at 21:04















up vote
0
down vote

favorite












I have a table table1 with the following columns:



id (INT)
label (VARCHAR)
value (INT)


I want to retrieve both the average of the value column, the entire row where the value is the largest, and the row with the lowest value. Something like this:



{
average: xxx,
maxval: {
id: x,
label: x,
value: x
},
minval: {
id: x,
label: x,
value: x
}
}


Tried with the following query:



SELECT 
AVG(a.value) AS average,
b.*,
c.*
FROM table1 a
INNER JOIN table1 b ON ( b.value = (SELECT MAX(value) FROM table1 WHERE label = "el"))
INNER JOIN table1 c ON ( c.value = (SELECT MIN(value) FROM table1 WHERE label = "el"))
WHERE a.label = "el";


but this will return all the fields in a single level, like this:



{
average,
id,
label,
value,
id,
label,
value
}


Tried this too:



SELECT 
AVG(a.value) AS average,
(SELECT b.* FROM table1 b WHERE ( b.value = (SELECT MAX(value) FROM table1 WHERE label = "el"))) AS maxval,
(SELECT c.* FROM table1 c WHERE ( c.value = (SELECT MIN(value) FROM table1 WHERE label = "el"))) AS minval
FROM table1 a
WHERE a.label = "el";


This last one give an error as I'm trying to use a single alias on multiple columns.



Using MySQL 5.5.










share|improve this question


















  • 2




    You cant return a composite result unless you use JSON
    – Juan Carlos Oropeza
    Nov 19 at 20:17










  • How are you returning that output from SQL? Are you using application layer like PHP?
    – Parfait
    Nov 19 at 20:47










  • @Parfait sending query from nodejs
    – neptune
    Nov 19 at 21:04













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a table table1 with the following columns:



id (INT)
label (VARCHAR)
value (INT)


I want to retrieve both the average of the value column, the entire row where the value is the largest, and the row with the lowest value. Something like this:



{
average: xxx,
maxval: {
id: x,
label: x,
value: x
},
minval: {
id: x,
label: x,
value: x
}
}


Tried with the following query:



SELECT 
AVG(a.value) AS average,
b.*,
c.*
FROM table1 a
INNER JOIN table1 b ON ( b.value = (SELECT MAX(value) FROM table1 WHERE label = "el"))
INNER JOIN table1 c ON ( c.value = (SELECT MIN(value) FROM table1 WHERE label = "el"))
WHERE a.label = "el";


but this will return all the fields in a single level, like this:



{
average,
id,
label,
value,
id,
label,
value
}


Tried this too:



SELECT 
AVG(a.value) AS average,
(SELECT b.* FROM table1 b WHERE ( b.value = (SELECT MAX(value) FROM table1 WHERE label = "el"))) AS maxval,
(SELECT c.* FROM table1 c WHERE ( c.value = (SELECT MIN(value) FROM table1 WHERE label = "el"))) AS minval
FROM table1 a
WHERE a.label = "el";


This last one give an error as I'm trying to use a single alias on multiple columns.



Using MySQL 5.5.










share|improve this question













I have a table table1 with the following columns:



id (INT)
label (VARCHAR)
value (INT)


I want to retrieve both the average of the value column, the entire row where the value is the largest, and the row with the lowest value. Something like this:



{
average: xxx,
maxval: {
id: x,
label: x,
value: x
},
minval: {
id: x,
label: x,
value: x
}
}


Tried with the following query:



SELECT 
AVG(a.value) AS average,
b.*,
c.*
FROM table1 a
INNER JOIN table1 b ON ( b.value = (SELECT MAX(value) FROM table1 WHERE label = "el"))
INNER JOIN table1 c ON ( c.value = (SELECT MIN(value) FROM table1 WHERE label = "el"))
WHERE a.label = "el";


but this will return all the fields in a single level, like this:



{
average,
id,
label,
value,
id,
label,
value
}


Tried this too:



SELECT 
AVG(a.value) AS average,
(SELECT b.* FROM table1 b WHERE ( b.value = (SELECT MAX(value) FROM table1 WHERE label = "el"))) AS maxval,
(SELECT c.* FROM table1 c WHERE ( c.value = (SELECT MIN(value) FROM table1 WHERE label = "el"))) AS minval
FROM table1 a
WHERE a.label = "el";


This last one give an error as I'm trying to use a single alias on multiple columns.



Using MySQL 5.5.







mysql sql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 20:02









neptune

628819




628819








  • 2




    You cant return a composite result unless you use JSON
    – Juan Carlos Oropeza
    Nov 19 at 20:17










  • How are you returning that output from SQL? Are you using application layer like PHP?
    – Parfait
    Nov 19 at 20:47










  • @Parfait sending query from nodejs
    – neptune
    Nov 19 at 21:04














  • 2




    You cant return a composite result unless you use JSON
    – Juan Carlos Oropeza
    Nov 19 at 20:17










  • How are you returning that output from SQL? Are you using application layer like PHP?
    – Parfait
    Nov 19 at 20:47










  • @Parfait sending query from nodejs
    – neptune
    Nov 19 at 21:04








2




2




You cant return a composite result unless you use JSON
– Juan Carlos Oropeza
Nov 19 at 20:17




You cant return a composite result unless you use JSON
– Juan Carlos Oropeza
Nov 19 at 20:17












How are you returning that output from SQL? Are you using application layer like PHP?
– Parfait
Nov 19 at 20:47




How are you returning that output from SQL? Are you using application layer like PHP?
– Parfait
Nov 19 at 20:47












@Parfait sending query from nodejs
– neptune
Nov 19 at 21:04




@Parfait sending query from nodejs
– neptune
Nov 19 at 21:04












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You need do separated querys for each one



SELECT q1.*, q2.*, q3.*
FROM (SELECT AVG(value)
FROM Table1) as q1
CROSS JOIN ( SELECT *
FROM Table1
ORDER BY value
LIMIT 1) as q2
CROSS JOIN ( SELECT *
FROM Table1
ORDER BY value DESC
LIMIT 1) as q3


You can use JSON_OBJECT(key1, value1, key2, value2, ... key(n), value(n))



 SELECT q1.*, 
JSON_OBJECT("id", q2.id, "label", q2.label,"value", q2.value),
JSON_OBJECT("id", q3.id, "label", q3.label,"value", q3.value)





share|improve this answer























  • With this I have the same issue as on my first try. All the resulted columns on a single level.
    – neptune
    Nov 19 at 20:18










  • Check my edit. Try with JSON_OBJECT
    – Juan Carlos Oropeza
    Nov 19 at 20:36












  • managed to do a similar workaround with CONCAT because JSON_OBJECT is not available in 5.5.
    – neptune
    Nov 19 at 21:05











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%2f53381827%2fgrouping-columns-from-a-nested-select%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
1
down vote



accepted










You need do separated querys for each one



SELECT q1.*, q2.*, q3.*
FROM (SELECT AVG(value)
FROM Table1) as q1
CROSS JOIN ( SELECT *
FROM Table1
ORDER BY value
LIMIT 1) as q2
CROSS JOIN ( SELECT *
FROM Table1
ORDER BY value DESC
LIMIT 1) as q3


You can use JSON_OBJECT(key1, value1, key2, value2, ... key(n), value(n))



 SELECT q1.*, 
JSON_OBJECT("id", q2.id, "label", q2.label,"value", q2.value),
JSON_OBJECT("id", q3.id, "label", q3.label,"value", q3.value)





share|improve this answer























  • With this I have the same issue as on my first try. All the resulted columns on a single level.
    – neptune
    Nov 19 at 20:18










  • Check my edit. Try with JSON_OBJECT
    – Juan Carlos Oropeza
    Nov 19 at 20:36












  • managed to do a similar workaround with CONCAT because JSON_OBJECT is not available in 5.5.
    – neptune
    Nov 19 at 21:05















up vote
1
down vote



accepted










You need do separated querys for each one



SELECT q1.*, q2.*, q3.*
FROM (SELECT AVG(value)
FROM Table1) as q1
CROSS JOIN ( SELECT *
FROM Table1
ORDER BY value
LIMIT 1) as q2
CROSS JOIN ( SELECT *
FROM Table1
ORDER BY value DESC
LIMIT 1) as q3


You can use JSON_OBJECT(key1, value1, key2, value2, ... key(n), value(n))



 SELECT q1.*, 
JSON_OBJECT("id", q2.id, "label", q2.label,"value", q2.value),
JSON_OBJECT("id", q3.id, "label", q3.label,"value", q3.value)





share|improve this answer























  • With this I have the same issue as on my first try. All the resulted columns on a single level.
    – neptune
    Nov 19 at 20:18










  • Check my edit. Try with JSON_OBJECT
    – Juan Carlos Oropeza
    Nov 19 at 20:36












  • managed to do a similar workaround with CONCAT because JSON_OBJECT is not available in 5.5.
    – neptune
    Nov 19 at 21:05













up vote
1
down vote



accepted







up vote
1
down vote



accepted






You need do separated querys for each one



SELECT q1.*, q2.*, q3.*
FROM (SELECT AVG(value)
FROM Table1) as q1
CROSS JOIN ( SELECT *
FROM Table1
ORDER BY value
LIMIT 1) as q2
CROSS JOIN ( SELECT *
FROM Table1
ORDER BY value DESC
LIMIT 1) as q3


You can use JSON_OBJECT(key1, value1, key2, value2, ... key(n), value(n))



 SELECT q1.*, 
JSON_OBJECT("id", q2.id, "label", q2.label,"value", q2.value),
JSON_OBJECT("id", q3.id, "label", q3.label,"value", q3.value)





share|improve this answer














You need do separated querys for each one



SELECT q1.*, q2.*, q3.*
FROM (SELECT AVG(value)
FROM Table1) as q1
CROSS JOIN ( SELECT *
FROM Table1
ORDER BY value
LIMIT 1) as q2
CROSS JOIN ( SELECT *
FROM Table1
ORDER BY value DESC
LIMIT 1) as q3


You can use JSON_OBJECT(key1, value1, key2, value2, ... key(n), value(n))



 SELECT q1.*, 
JSON_OBJECT("id", q2.id, "label", q2.label,"value", q2.value),
JSON_OBJECT("id", q3.id, "label", q3.label,"value", q3.value)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 20:35

























answered Nov 19 at 20:13









Juan Carlos Oropeza

35.6k63675




35.6k63675












  • With this I have the same issue as on my first try. All the resulted columns on a single level.
    – neptune
    Nov 19 at 20:18










  • Check my edit. Try with JSON_OBJECT
    – Juan Carlos Oropeza
    Nov 19 at 20:36












  • managed to do a similar workaround with CONCAT because JSON_OBJECT is not available in 5.5.
    – neptune
    Nov 19 at 21:05


















  • With this I have the same issue as on my first try. All the resulted columns on a single level.
    – neptune
    Nov 19 at 20:18










  • Check my edit. Try with JSON_OBJECT
    – Juan Carlos Oropeza
    Nov 19 at 20:36












  • managed to do a similar workaround with CONCAT because JSON_OBJECT is not available in 5.5.
    – neptune
    Nov 19 at 21:05
















With this I have the same issue as on my first try. All the resulted columns on a single level.
– neptune
Nov 19 at 20:18




With this I have the same issue as on my first try. All the resulted columns on a single level.
– neptune
Nov 19 at 20:18












Check my edit. Try with JSON_OBJECT
– Juan Carlos Oropeza
Nov 19 at 20:36






Check my edit. Try with JSON_OBJECT
– Juan Carlos Oropeza
Nov 19 at 20:36














managed to do a similar workaround with CONCAT because JSON_OBJECT is not available in 5.5.
– neptune
Nov 19 at 21:05




managed to do a similar workaround with CONCAT because JSON_OBJECT is not available in 5.5.
– neptune
Nov 19 at 21:05


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381827%2fgrouping-columns-from-a-nested-select%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

Feedback on college project

Futebolista

Albești (Vaslui)