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.
mysql sql
add a comment |
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.
mysql sql
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
add a comment |
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.
mysql sql
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
mysql sql
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
add a comment |
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
add a comment |
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)
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 withJSON_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
add a comment |
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)
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 withJSON_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
add a comment |
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)
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 withJSON_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
add a comment |
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)
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)
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 withJSON_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
add a comment |
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 withJSON_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
add a comment |
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.
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%2f53381827%2fgrouping-columns-from-a-nested-select%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
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