Solved

Sorting a Union Query

Posted on 2007-11-19
7
1,193 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
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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Results to Excel File 18 76
How to use three values with DATEDIFF 3 43
SQL Recursion schedule 13 35
How to structure query with count aggregate 4 47
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…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 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