I hope you are doing well,
I am working on aggregating data by month,
Note: I am querying data from Redhsift using Metabase.
This is my query:
(SELECT
dataset.value AS value,
TO_CHAR(dataset.time ::date,'MM/YYYY') AS time,
COUNT(id) AS count
FROM
dataset
GROUP BY
dataset.value, TO_CHAR(dataset.time ::date,'MM/YYYY')
ORDER BY
dataset.value ASC, TO_CHAR(dataset.time ::date,'MM/YYYY') ASC)
UNION ALL
(SELECT
dataset_2.value_2 AS x,
TO_CHAR(dataset_2.time ::date,'MM/YYYY') AS time,
COUNT(id) AS count
FROM
dataset_2
GROUP BY
dataset_2.value_2, TO_CHAR(dataset_2.time ::date,'MM/YYYY')
ORDER BY
dataset_2.value_2in ASC, TO_CHAR(dataset_2.time ::date,'MM/YYYY') ASC)
However this query converts date into string. Hence the output is not in proper order. Eg: Jan 22, Jan 23, Feb 22, March 23 instead of Jan 22, Feb 22, March 22 and so on.
Below are the screenshot of two sample dataset.
Screenshot of the Desired Output:
Requesting your help improve the query so the date aggregated by month is in ascending order. Thanks in advance.
You need to just switch the
ORDER
clauseResult