Reverse Order of SQL Query Issue

#1

I’m having an issue using ORDER BY in my SQL code. This SQL code is contained in a Power Table. My intention is to return the values in order of the record date descending, however once I add this line to the code it results in an error.
Please view my code below and if anybody could help it would be great.
Many thanks,
Cathal.

(Working Block of Code Above)

UNION

SELECT
record_date,
data1,
data2,
data3,
data4,
data5,
data6
FROM daily_totals_kwh
WHERE record_date BETWEEN ‘{SQL/5DaysAgo}’ AND ‘{SQL/now}’

UNION

(Working Block of Code Below)

0 Likes

#2

Try running just that one block by itself. Also, UNION only allows an ORDER BY clause for the entire result, at the end.

1 Like

#3

Many thanks for your response. The block runs successfully on its own, however I require the information to be presented in the opposite order from how it is currently presenting itself.
Do you know how this could be done?

0 Likes

#4

Lazy way of doing this:

Select * from (
(Working Block of Code Above)

UNION

SELECT
record_date,
data1,
data2,
data3,
data4,
data5,
data6
FROM daily_totals_kwh
WHERE record_date BETWEEN ‘{SQL/5DaysAgo}’ AND ‘{SQL/now}’

UNION

(Working Block of Code Below)
) unionedData
order by record_date desc
0 Likes

#5

Without seeing exactly what you are doing and what you want it to return, I don’t know how to help you.

This is functionally identical to just adding the Order By at the end without nesting the union in a subquery.

0 Likes