Solved

Sorting a Union Query

Posted on 2007-11-19
7
1,192 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
ID: 20317027
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
ID: 20317941
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
ID: 31410044
I am trying to now export the query into a txt file and  I get the error too few parameters.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 1

Expert Comment

by:Vee_Mod
ID: 20324978
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
ID: 20327937
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
ID: 20328519
> "Order By 1" will produce an error in Access.

no, it will not.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 20337300
@vadimrapp1 ,
>no, it will not

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Syntax 5 39
Sql server insert 13 36
performance query 4 32
SQL Syntax Grouping Sum question 7 27
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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