We help IT Professionals succeed at work.
Get Started

SQL -  Srting multiple merged queries

48 Views
Last Modified: 2016-04-05
I'm trying to automate multiple reports into a single email/report that has multiple top 10 lists.  I'm able to merge the report and insert individual title lines. But I can't sort them properly. If I run the queries one at a time and sort/ ORDER By they come out properly. but the merged reports won't let me use a sort/Order by option.

The query below works but it shows the top 10 customers alphabetically not be descending value. if I add (ORDER BY sum (T1.[LineTotal]) desc) to the end I get errors. How can I get the 2 top 10 lists to sort differently.


SELECT cast (NULL as varchar),'Top 10 customers for 2015'

UNION all

SELECT top 10 sum (T1.[LineTotal]), T0.[CardName] FROM OINV T0  INNER JOIN INV1 T1 ON T0.[DocEntry] = T1.[DocEntry] WHERE T0.[DocDate] > '01/01/2015' GROUP BY T0.[CardName] 

union all

SELECT cast (NULL as varchar),'Top 10 customers for 2014'

UNION all

SELECT top 10 sum (T1.[LineTotal]), T0.[CardName] FROM OINV T0  INNER JOIN INV1 T1 ON T0.[DocEntry] = T1.[DocEntry] WHERE T0.[DocDate] between '01/01/2014' and '01/01/2015' GROUP BY T0.[CardName] 

Open in new window

Comment
Watch Question
Information Technology Specialist
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE