?
Solved

Sorting a Union Query

Posted on 2007-11-19
7
Medium Priority
?
1,200 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

770 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