Secondary Sort for Union Query?

M

Monish

I have a Union of 4 "TOP" queries, each of which is ordered as per the TOP
function, by Field A.

I need the output of this Union Query to sort first by Field B, then by
Field A

How do I set the sort for the entire Union - having already set a sort for
each part
of the union?

Below is a part of my Union Query:

SELECT TOP 5 [Account Name], Eligibles, Description, [Mail Date], [Week
Tracker] AS WeekNumber
FROM QryWeeklyReportDroppedML
WHERE [Week Tracker] = 'Current Week'
ORDER BY Eligibles DESC, [Mail Date]
UNION ALL
SELECT TOP 5 [Account Name], Eligibles, Description, [Mail Date], [Week
Tracker] AS WeekNumber
FROM QryWeeklyReportDroppedML
WHERE [Week Tracker] = 'Week - 1'
ORDER BY Eligibles DESC, [Mail Date]

Where I need to sort the output by Mail Date and then within that by Eligibles

Thanks for taking the time to read this and thanks in advance for any
assistance!
 
T

Tom Ellison

Dear Monish:

Don't bother sorting the individual parts. Sort only the last one. That
determines the sort for the entire UNION query.

Tom Ellison
 
M

Monish

Thanks Tom.

My concern is, however, that I need each part to return the Top 5 records by
Eligibles - which is why I am sorting each one by Eligibles DESC

Your thoughts are appreciated.

Monish

Tom Ellison said:
Dear Monish:

Don't bother sorting the individual parts. Sort only the last one. That
determines the sort for the entire UNION query.

Tom Ellison


Monish said:
I have a Union of 4 "TOP" queries, each of which is ordered as per the TOP
function, by Field A.

I need the output of this Union Query to sort first by Field B, then by
Field A

How do I set the sort for the entire Union - having already set a sort for
each part
of the union?

Below is a part of my Union Query:

SELECT TOP 5 [Account Name], Eligibles, Description, [Mail Date], [Week
Tracker] AS WeekNumber
FROM QryWeeklyReportDroppedML
WHERE [Week Tracker] = 'Current Week'
ORDER BY Eligibles DESC, [Mail Date]
UNION ALL
SELECT TOP 5 [Account Name], Eligibles, Description, [Mail Date], [Week
Tracker] AS WeekNumber
FROM QryWeeklyReportDroppedML
WHERE [Week Tracker] = 'Week - 1'
ORDER BY Eligibles DESC, [Mail Date]

Where I need to sort the output by Mail Date and then within that by
Eligibles

Thanks for taking the time to read this and thanks in advance for any
assistance!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

SUM in a UNION query 2
Union Query 1
UNION question 3
Union Query of Two Queries (Part 2) 2
Union query 5
Access Query - returns 5 and 7 day information 0
union query sort 3
Union Query - Group & Sum 1

Top