自定义SORT BY SQL

时间:2017-02-28 13:48:36

标签: sql sorting ms-access-2013

我是社区的新手,但过去曾多次引用它。我有一个问题,我试图在Access中克服,特别是SQL中的SORT BY问题。

长话短说,我需要根据几个不同查询的结果创建一个报告。我使用了Union查询来解决“Query is too complex”问题。但是,查询的结果不是我想要的顺序。

由于此UNION查询不是基于一个特定的表,而是基于许多查询的结果,因此我无法按特定列标题进行排序。

我想按照它们在SQL语句中编写的方式对结果进行排序。谁能提供一些有关如何做到这一点的见解?我尝试了几种不同的方法,但最终总是出现错误信息。这是代码,非常感谢任何帮助。

SELECT [Aqua-Anvil_Total].Expr1
FROM [Aqua-Anvil_Total];
UNION SELECT [Aqua-Reslin_Total].Expr1
FROM [Aqua-Reslin_Total];
UNION SELECT [Aqua_Zenivex_Total].Expr1
FROM [Aqua_Zenivex_Total];
UNION SELECT [Aqualuer_20-20_Total].Expr1
FROM [Aqualuer_20-20_Total];
UNION SELECT [Avalon_Total].Expr1
FROM [Avalon_Total];
UNION SELECT [BVA_13_Total].Expr1
FROM [BVA_13_Total];
UNION SELECT [Deltagard_Total].Expr1
FROM [Deltagard_Total];
UNION SELECT [Envion_Total].Expr1
FROM [Envion_Total];
UNION SELECT [Scourge_18-54_Total].Expr1
FROM [Scourge_18-54_Total];
UNION SELECT [Zenivex_E20_Total].Expr1
FROM [Zenivex_E20_Total];

1 个答案:

答案 0 :(得分:0)

这使用union all代替union,因此如果您使用union删除重复项,那么在此之后还有更多工作要做。

select Expr1 
from (
  select [Aqua-Anvil_Total].Expr1, 0 as sort
  from [Aqua-Anvil_Total]
  union all select [Aqua-Reslin_Total].Expr1, 1 as sort
  from [Aqua-Reslin_Total]
  union all select [Aqua_Zenivex_Total].Expr1, 2 as sort
  from [Aqua_Zenivex_Total]
  union all select [Aqualuer_20-20_Total].Expr1, 3 as sort
  from [Aqualuer_20-20_Total]
  union all select [Avalon_Total].Expr1, 4 as sort
  from [Avalon_Total]
  union all select [bva_13_Total].Expr1, 5 as sort
  from [bva_13_Total]
  union all select [Deltagard_Total].Expr1, 6 as sort
  from [Deltagard_Total]
  union all select [Envion_Total].Expr1, 7 as sort
  from [Envion_Total]
  union all select [Scourge_18-54_Total].Expr1, 8 as sort
  from [Scourge_18-54_Total]
  union all select [Zenivex_E20_Total].Expr1, 9 as sort
  from [Zenivex_E20_Total]
) as u
order by u.sort