简体   繁体   中英

MS Access Union Query Order by Select statements

I am attempting to Union 3 different pipe delimited tables into one so that I can export as one text file. Each pipe delimited table has a different structure. Here is what I have so far. When I run the Union Query it is sorting it by Query 2, Query 1, Query 3; which I am sure it is because of the output of each query is doing a final sort on ABC order. Is there any way to sort it by the order of the query?

 SELECT  qry_Header.HDR  
 from qry_Header

 union

 select qry_GHD_Headers.GHD_Header  
 from qry_GHD_Headers inner join [Coverage ID] ON qry_GHD_Headers.      
 [Coverage ID] = [Coverage ID].[Coverage ID]
 where [Coverage ID].ID = 1

 union


 select 
 [qry_Pipe_Delim].Pipe_Delim 
 FROM qry_Pipe_Delim INNER JOIN [Coverage ID] ON qry_Pipe_Delim.
 [Coverage ID] = [Coverage ID].[Coverage ID]
 where [Coverage ID].ID = 1

Output looks like this:

 GHD|CA015015HL|TM|F|20150414
 HDR|10|P00000000020123|AXQEN16E88|RXHUB|OPTUMRX|CP000005|20150415|104818|FRM|U|20150414|P
 TDT|A|CA015015HL|00008112312|003|||||This medication increases risk of serious toxicity in patients 65 and older. Use safer alternatives.|National guidelines and quality measures recommend avoiding this medication in patients 65 yrs and older due to incre

Preferred output:

 HDR|10|P00000000020123|AXQEN16E88|RXHUB|OPTUMRX|CP000005|20150415|104818|FRM|U|20150414|P
 GHD|CA015015HL|TM|F|20150414
 TDT|A|CA015015HL|00008112312|003|||||This medication increases risk of serious toxicity in patients 65 and older. Use safer alternatives.|National guidelines and quality measures recommend avoiding this medication in patients 65 yrs and older due to incre

I figured it out - at least a workaround that works

 SELECT HDR
 FROM(
 SELECT  1, qry_Header.HDR  
 from qry_Header

 union

  select 2, qry_GHD_Headers.GHD_Header  
  from qry_GHD_Headers inner join [Coverage ID] ON qry_GHD_Headers.      
  [Coverage ID] = [Coverage ID].[Coverage ID]
  where [Coverage ID].ID = 1

  union


 select 3,
 [qry_Pipe_Delim].Pipe_Delim 
 FROM qry_Pipe_Delim INNER JOIN [Coverage ID] ON qry_Pipe_Delim.
 [Coverage ID] = [Coverage ID].[Coverage ID]
 where [Coverage ID].ID = 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM