I have simple table as shown below,
datetime | Rupee | Pound |
---|---|---|
2019-02-16 | 80% | 70% |
2019-02-17 | 30% | 60% |
2019-02-18 | 67% | 89% |
And MySQL query looks like below,
SELECT json_arrayagg(obj)
FROM (
SELECT json_object(
'currency', 'rupee',
'data', json_arrayagg(json_array(datetime, rupee))
) AS obj
FROM currency
UNION ALL
SELECT json_object(
'currency', 'pound',
'data', json_arrayagg(json_array(datetime, pound))
)
FROM currency
) x;
Result
[{"currency": "rupee", "data": [["2019-02-16", "80%"], ["2019-02-17", "30%"], ["2019-02-18", "67%"]]},
{"currency": "pound", "data": [["2019-02-16", "70%"], ["2019-02-17", "60%"], ["2019-02-18", "89%"]]}]
The result is good with no problem.
The thing I want is data
to come in ASC|DESC
order on Rupee or Pound column.
The expected result would look like below,
[{"currency": "Rupee", "data": [["2019-02-17", "30%"], ["2019-02-18", "67%"], ["2019-02-16", "80%"]]},
{"currency": "pound", "data": [["2019-02-17", "60%"], ["2019-02-16", "70%"], ["2019-02-18", "89%"]]}]
I know order by but here where to apply it I don’t know. I don’t know even if it is possible in query itself.
Also, please note that Rupee & Pound columns are type of varchar(10)
DB Fiddle with sample data: https://dbfiddle.uk/_9g-CSjH
2
Answers
You can’t control the order of the elements in the array using
json_arrayagg()
.You can use
group_concat()
to manually generate the the ordered json arrays :Result :
Demo here
The
json_arrayagg
cannot sort the data before aggregation. However, thegroup_concat
function allows sorting the data inside each group before concatenation. You need to apply some manual JSON generation i.e. adding square brackets and commas:DB<>Fiddle