What's Wrong with My ORDER BY Clause???

leishtheman

New Member
Joined
Oct 1, 2007
Messages
32
Hello

I'm hoping somebody can educate me around this puzzle.

I've been trying to sort descending on a basic calculated field within a basic query. The ORDER BY clause refuses to work however (keeps prompting for parameter values to be entered) unless I use the field position indicator...why??? All the advice I've googled suggests I'm doing it correctly...

refuses to work:

SELECT SUPPLIER, SUM(UN.Y1) AS T_Y1, SUM(UN.Y2) AS T_Y2, (T_Y2-T_Y1) AS DIFF

FROM (

SELECT SUPPLIER, SPEND AS Y1, 0 AS Y2
FROM Table1
WHERE YR = 2015

UNION

SELECT SUPPLIER, 0 AS Y1, SPEND AS Y2
FROM Table1
WHERE YR = 2016) AS UN

GROUP BY SUPPLIER

ORDER BY
(T_Y2-T_Y1) DESC


...works fine:


SELECT SUPPLIER, SUM(UN.Y1) AS T_Y1, SUM(UN.Y2) AS T_Y2, (T_Y2-T_Y1) AS DIFF

FROM (

SELECT SUPPLIER, SPEND AS Y1, 0 AS Y2
FROM Table1
WHERE YR = 2015

UNION

SELECT SUPPLIER, 0 AS Y1, SPEND AS Y2
FROM Table1
WHERE YR = 2016) AS UN

GROUP BY SUPPLIER

ORDER BY
4 DESC


Explanation would be most appreciated.

Many thanks,
Andy
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I am pretty sure that you cannot use mathematical computations or aliases in your ORDER BY clause.
You either need to use a Field Name (if Table field and not calculated field), or the ordinal field number (being returned in the query).
I always use the number, like you did in your second example when using UNION queries.
 
Upvote 0
Note that when googling for SQL you have to be sure that the results are specifically for Access, not SQL in general (there are many examples that would work in SQL Server that wouldn't work in Access). Generally, you have to accept a few Access quirks.

I don't see a query field that is the result of two other aggregate function fields in the same Select clause very often ... SELECT T_Y1, T_Y2, (T_Y2 - T_Y1) AS Diff ... so in this case example are probably rare and you probably want to just go with what works.

For order by issues in general, if you have order by problems you can always wrap everything in a virtual table and then order that:
Code:
select T.a, T.b, T.c
from 
(
.... lots of sql stuff
) as T
order by T.a, T.b, T.c
 
Upvote 0
Thanks guys. I figured it out yesterday in the end, needed to remove references to all alias's formed in the top level query:


SELECT SUPPLIER, SUM(UN.Y1) AS Y1, SUM(UN.Y2) AS Y2, Y2-Y1 AS DIFF
FROM (SELECT SUPPLIER, SPEND AS Y1, 0 AS Y2
FROM Table1
WHERE YR = 2015

UNION

SELECT SUPPLIER, 0 AS Y1, SPEND AS Y2
FROM Table1
WHERE YR = 2016) AS UN
GROUP BY SUPPLIER
ORDER BY (SUM(UN.Y1) - SUM(UN.Y2)) DESC;


Andy
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top