MS Access Union查询按Select语句排序

时间:2015-04-14 17:50:44

标签: sql ms-access union

我正在尝试将3个不同的管道分隔表合并为一个,以便我可以导出为一个文本文件。每个管道分隔表具有不同的结构。这是我到目前为止所拥有的。当我运行Union Query时,它将按查询2,查询1,查询3进行排序;我确信这是因为每个查询的输出都是在ABC订单上做最后的排序。有没有办法按查询顺序对其进行排序?

 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

输出如下:

 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

首选输出:

 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

1 个答案:

答案 0 :(得分:0)

我弄清楚了 - 至少是一种有效的解决方法

 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)