# Reverse Order of SQL Query Issue

**URL:** <https://forum.inductiveautomation.com/t/reverse-order-of-sql-query-issue/23781>\
**Category:** General Discussion\
**Created:** [April 2, 2019, 12:04pm UTC](https://forum.inductiveautomation.com/t/reverse-order-of-sql-query-issue/23781 "2019-04-02T12:04:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cathal](https://avatars.discourse-cdn.com/v4/letter/c/df788c/32.png) [@cathal](https://forum.inductiveautomation.com/u/cathal)\
**Post date:** [April 2, 2019, 12:04pm UTC](https://forum.inductiveautomation.com/t/reverse-order-of-sql-query-issue/23781/1 "2019-04-02T12:04:36Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 2, 2019, 12:45pm UTC](https://forum.inductiveautomation.com/t/reverse-order-of-sql-query-issue/23781/2 "2019-04-02T12:45:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cathal](https://avatars.discourse-cdn.com/v4/letter/c/df788c/32.png) [@cathal](https://forum.inductiveautomation.com/u/cathal)\
**Post date:** [April 2, 2019, 2:36pm UTC](https://forum.inductiveautomation.com/t/reverse-order-of-sql-query-issue/23781/3 "2019-04-02T14:36:13Z")

</div>

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?

> [@pturmel](#):
>
> Try running just that one block by itself. Also, UNION only allows an ORDER BY clause for the entire result, at the end.

---

<div class="post-metadata">

**Author:** ![Stevenson.Yuan](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Stevenson.Yuan](https://forum.inductiveautomation.com/u/Stevenson.Yuan)\
**Post date:** [April 2, 2019, 2:39pm UTC](https://forum.inductiveautomation.com/t/reverse-order-of-sql-query-issue/23781/4 "2019-04-02T14:39:27Z")

</div>

Lazy way of doing this:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 2, 2019, 3:48pm UTC](https://forum.inductiveautomation.com/t/reverse-order-of-sql-query-issue/23781/5 "2019-04-02T15:48:46Z")

</div>

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

> [@Stevenson.Yuan](#):
>
> Select \* from (.....) unionedData  
> order by record\_date desc

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