Solved

Sorting a Union Query

Posted on 2007-11-19
7
1,197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

705 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