Solved

Sorting a Union Query

Posted on 2007-11-19
7
1,183 Views
Last Modified: 2010-04-21
I created a Union query as Follows:
SELECT Format(Now(),"mmddyyyy") AS NOMBREASEGURADO, [Seguros Banamex Header table].APELLIDOASEGURADO AS APELLIDOASEGURADO, "" AS NOMBREBENEFICIARIO, "" AS APELLIDOBENEFICIARIO, "" AS MONTOREMESA, "" AS [FECHA-REMESA], "" AS Identificadorderemesa
FROM [Seguros Banamex Header table];
union
SELECT dbo_MFS_Payments.SndFirstName AS NOMBREASEGURADO, dbo_MFS_Payments.SndLastName AS APELLIDOASEGURADO, dbo_MFS_Payments.RecFirstName AS NOMBREBENEFICIARIO, dbo_MFS_Payments.RecLastName AS APELLIDOBENEFICIARIO, [RecAmount]/100 AS MONTOREMESA, Format(dbo_WU_SettlementFile!PaidDate,"mmddyyyy") AS [FECHA-REMESA], dbo_MFS_Payments.MTCN AS Identificadorderemesa
FROM dbo_MFS_Payments INNER JOIN dbo_WU_SettlementFile ON dbo_MFS_Payments.MTCN = dbo_WU_SettlementFile.MTCN
WHERE ((([dbo_WU_SettlementFile]![PaidDate]) Between [what is the start date] And [what is the end date]) AND ((dbo_WU_SettlementFile.RecordType)="1" Or (dbo_WU_SettlementFile.RecordType)="3"))
UNION SELECT Count(dbo_MFS_Payments.MTCN) AS NOMBREASEGURADO, Sum([RecAmount]/100) AS APELLIDOASEGURADO, "" AS NOMBREBENEFICIARIO, "" AS APELLIDOBENEFICIARIO, "" AS MONTOREMESA,  "" AS [FECHA-REMESA], "" AS Identificadorderemesa
FROM dbo_MFS_Payments INNER JOIN dbo_WU_SettlementFile ON dbo_MFS_Payments.MTCN = dbo_WU_SettlementFile.MTCN
WHERE ((([dbo_WU_SettlementFile]![PaidDate]) Between [what is the start date] And [what is the end date]) AND ((dbo_WU_SettlementFile.RecordType)="1" Or (dbo_WU_SettlementFile.RecordType)="3"));


The results are as follows:

NOMBREASEGURADO      APELLIDOASEGURADO      NOMBREBENEFICIARIO      APELLIDOBENEFICIARIO      MONTOREMESA      FECHA-REMESA      Identificadorderemesa
11192007       1234456789                              
4942      19239324                              
WILLIE      A BANUELOS      LUIS GUILLERMO      BANUELOS GARCIA      1068      10012007      2200522473
JOSE      A MEDINA      JOSE      SABINO GARCIA MONDRAGON      13884      10012007      4888956494
MARCO      A SIFUENTES      ANA ROSA      SIFUENTES SIFUENTES      1078      10012007      1580203253
JORGE      A SOSA      MARIANA      SOSA SANCHEZ      2564      10012007      5130503021
APOLONIO      ABARCA      ALEXIS J      ABARCA GAZGA      537      10012007      4852777720
CARLOS      ABEJA GUTIERREZ      EPIFANIO      ABEJAR RODEIGUEZ      10680      10012007      1862047513
FELIPE      ABRAHAM MELQUIADES      GREGORIA      AGUILAR MELQUIADES      3204      10012007      2491614150

The above is just an example of the results.  What I need to do is have this always as the first line:
11192007       1234456789
and
the next should always be at the bottom:
4942      19239324

The first line is my header and the next is the footer but I can't get the the second line to go to the bottom using  a sort.  

Any suggestions would be greatly appreciated.

thks
Ken
4942      19239324
0
Comment
Question by:kdr2003
7 Comments
 
LVL 40

Expert Comment

by:Vadim Rapp
Comment Utility
Add one more column and order by that column:

select

0, <your 1st select>
union
1,<your 2nd select>
union
2,<your 3rd select>

order by 1

So the line that you want 1st, has 0 and thus goes first, everything else has 1 and goes 2nd, then the last line has 2 and goes last.
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
Comment Utility
which in your case would be
SELECT 1 as forsort, Format(Now(),"mmddyyyy") AS NOMBREASEGURADO, [Seguros Banamex Header table].APELLIDOASEGURADO AS APELLIDOASEGURADO, "" AS NOMBREBENEFICIARIO, "" AS APELLIDOBENEFICIARIO, "" AS MONTOREMESA, "" AS [FECHA-REMESA], "" AS Identificadorderemesa

FROM [Seguros Banamex Header table];

union

SELECT 2, dbo_MFS_Payments.SndFirstName AS NOMBREASEGURADO, dbo_MFS_Payments.SndLastName AS APELLIDOASEGURADO, dbo_MFS_Payments.RecFirstName AS NOMBREBENEFICIARIO, dbo_MFS_Payments.RecLastName AS APELLIDOBENEFICIARIO, [RecAmount]/100 AS MONTOREMESA, Format(dbo_WU_SettlementFile!PaidDate,"mmddyyyy") AS [FECHA-REMESA], dbo_MFS_Payments.MTCN AS Identificadorderemesa

FROM dbo_MFS_Payments INNER JOIN dbo_WU_SettlementFile ON dbo_MFS_Payments.MTCN = dbo_WU_SettlementFile.MTCN

WHERE ((([dbo_WU_SettlementFile]![PaidDate]) Between [what is the start date] And [what is the end date]) AND ((dbo_WU_SettlementFile.RecordType)="1" Or (dbo_WU_SettlementFile.RecordType)="3"))

UNION 3, SELECT Count(dbo_MFS_Payments.MTCN) AS NOMBREASEGURADO, Sum([RecAmount]/100) AS APELLIDOASEGURADO, "" AS NOMBREBENEFICIARIO, "" AS APELLIDOBENEFICIARIO, "" AS MONTOREMESA,  "" AS [FECHA-REMESA], "" AS Identificadorderemesa

FROM dbo_MFS_Payments INNER JOIN dbo_WU_SettlementFile ON dbo_MFS_Payments.MTCN = dbo_WU_SettlementFile.MTCN

WHERE ((([dbo_WU_SettlementFile]![PaidDate]) Between [what is the start date] And [what is the end date]) AND ((dbo_WU_SettlementFile.RecordType)="1" Or (dbo_WU_SettlementFile.RecordType)="3"));

ORDER BY forsort

Open in new window

0
 

Author Closing Comment

by:kdr2003
Comment Utility
I am trying to now export the query into a txt file and  I get the error too few parameters.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:Vee_Mod
Comment Utility
kdr2003 - please explain your answer selection.
It would appear that vadimrapp1 gave the same/similar information as was contained in the code snippet.

Your response would be appreciated.


Vee_Mod
Experts Exchange Moderator
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
An important distinction between these two answers is in the Order By clause.  "Order By 1" will produce an error in Access.  The second post avoids that issue by aliasing the first column and using the alias "[forsort]" as a valid field name in the Order By clause.

mbizup
Access Zone Advisor/Page Editor
0
 
LVL 40

Expert Comment

by:Vadim Rapp
Comment Utility
> "Order By 1" will produce an error in Access.

no, it will not.
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
@vadimrapp1 ,
>no, it will not

I owe you an apology.  The problem was in my sample database.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now