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