Secondary Sort for Union Query?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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!
 
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
 
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!
 
Back
Top