SQL order by when using a Union

Hello, I am using MS-SQL 2005.
I have this query that pulls invoice totals. It works fine, but.... if I want to include the History table with a Union All I can not get it to sort "Order By" properly.

This works fine....
SELECT
armast.custno AS 'Customer#',
arcust.company AS 'Company',
armast.salesmn AS 'Salesman',
CONVERT (varchar(50), CONVERT (money, SUM(armast.invamt)), 1) AS 'Invoice Total'
FROM armast
LEFT OUTER JOIN arcust ON arcust.custno = armast.custno
WHERE armast.invdte >= '2010-03-01'
 AND armast.invdte <= '2010-03-31'
 AND armast.invamt <> 0 and armast.arstat <> 'V' and armast.invno <> '_RECEIPT'
GROUP BY armast.custno, arcust.company, armast.salesmn
order by sum(armast.invamt) desc

This does not work I get the error
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.

SELECT
armast.custno AS 'Customer#',
arcust.company AS 'Company',
armast.salesmn AS 'Salesman',
CONVERT (varchar(50), CONVERT (money, SUM(armast.invamt)), 1) AS 'Invoice Total'
FROM armast
LEFT OUTER JOIN arcust ON arcust.custno = armast.custno
WHERE armast.invdte >= '2010-03-01'
 AND armast.invdte <= '2010-03-31'
 AND armast.invamt <> 0 and armast.arstat <> 'V' and armast.invno <> '_RECEIPT'
GROUP BY armast.custno, arcust.company, armast.salesmn

Union All

SELECT
arymst.custno AS 'Customer#',
arcust.company AS 'Company',
arymst.salesmn AS 'Salesman',
CONVERT (varchar(50), CONVERT (money, SUM(arymst.invamt)), 1) AS 'Invoice Total'
FROM arymst
LEFT OUTER JOIN arcust ON arcust.custno = arymst.custno
WHERE arymst.invdte >= '2010-03-01'
 AND arymst.invdte <= '2010-03-31'
 AND arymst.invamt <> 0 and arymst.arstat <> 'V' and arymst.invno <> '_RECEIPT'
GROUP BY arymst.custno, arcust.company, arymst.salesmn
order by sum(invamt) desc

How can I get this to order the amts correctly?

Thanks
LVL 1
Wildone63Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chapmandewCommented:
select * from (
SELECT
armast.custno AS 'Customer#',
arcust.company AS 'Company',
armast.salesmn AS 'Salesman',
CONVERT (varchar(50), CONVERT (money, SUM(armast.invamt)), 1) AS 'Invoice Total',
sum(invamt) as SortAmount
FROM armast
LEFT OUTER JOIN arcust ON arcust.custno = armast.custno
WHERE armast.invdte >= '2010-03-01'
 AND armast.invdte <= '2010-03-31'
 AND armast.invamt <> 0 and armast.arstat <> 'V' and armast.invno <> '_RECEIPT'
GROUP BY armast.custno, arcust.company, armast.salesmn

Union All

SELECT
arymst.custno AS 'Customer#',
arcust.company AS 'Company',
arymst.salesmn AS 'Salesman',
CONVERT (varchar(50), CONVERT (money, SUM(arymst.invamt)), 1) AS 'Invoice Total',
sum(invamt) as SortAmount
FROM arymst
LEFT OUTER JOIN arcust ON arcust.custno = arymst.custno
WHERE arymst.invdte >= '2010-03-01'
 AND arymst.invdte <= '2010-03-31'
 AND arymst.invamt <> 0 and arymst.arstat <> 'V' and arymst.invno <> '_RECEIPT'
GROUP BY arymst.custno, arcust.company, arymst.salesmn
) a
order by sortamount asc
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wildone63Author Commented:
Brilliant!!!
Thank You very much!
0
Brendt HessSenior DBACommented:
The simplest way to order a UNION is with the ordinal value of the column, e.g.:

ORDER BY 4

However, as you are outputting a formatted (varchar) version, this simple version will not work.  So, you need to add a level of indirection to your code, such as by creating a derived table, as in the example below:


SELECT 
	custno AS 'Customer#',
	company AS 'Company',
	salesmn AS 'Salesman',
	CONVERT (varchar(50), CONVERT (money, invamt), 1) AS 'Invoice Total'
FROM (
	SELECT
		armast.custno,
		arcust.company,
		armast.salesmn,
		SUM(armast.invamt) AS invamt
	FROM armast
	LEFT OUTER JOIN arcust 
		ON arcust.custno = armast.custno
	WHERE armast.invdte >= '2010-03-01'
		AND armast.invdte <= '2010-03-31'
		AND armast.invamt <> 0 and armast.arstat <> 'V' and armast.invno <> '_RECEIPT'
	GROUP BY armast.custno, arcust.company, armast.salesmn

	Union All

	SELECT
		arymst.custno,
		arcust.company,
		arymst.salesmn,
		SUM(arymst.invamt)
	FROM arymst
	LEFT OUTER JOIN arcust 
		ON arcust.custno = arymst.custno
	WHERE arymst.invdte >= '2010-03-01'
		AND arymst.invdte <= '2010-03-31'
		AND arymst.invamt <> 0 and arymst.arstat <> 'V' and arymst.invno <> '_RECEIPT'
	GROUP BY arymst.custno, arcust.company, arymst.salesmn
	) Src
ORDER BY invamt
--- alternate: Order By 4

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.