如何将单独的 SQL 个查询(每个查询都带有 ORDER BY)组合成一个查询?

How can I combine Separate SQL queries, Each With ORDER BYs Into One Query?

理论上,我想基本上创建两个具有相同列的单独表格,根据需要分别对它们进行排序,然后简单地将一个放在另一个下面并保持该顺序。

我已经尝试使用其他地方建议的方法(见下文)来做到这一点,例如:

SELECT * FROM
(SELECT [company name], [appointment call back 1], [appointment call back 2], [appointment date 1], [appointment date 2] FROM Vantrack_Tulsa WHERE [appointment call back 1] BETWEEN '6/1/2016' AND '6/1/2017' 
                          OR [appointment call back 2] BETWEEN '6/1/2016' AND '6/1/2017' 
                          ORDER BY [company name] ASC) t
                          UNION ALL
SELECT * FROM
(SELECT [company name], [appointment call back 1], [appointment call back 2], [appointment date 1], [appointment date 2] FROM Vantrack_Tulsa WHERE [appointment date 1] BETWEEN '6/1/2016' AND '6/1/2017'
                          OR [appointment date 2] BETWEEN '6/1/2016' AND '6/1/2017'
                          ORDER BY [company name] ASC) s

但我得到:

Msg 1033, Level 15, State 1, Line 8
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
Msg 1033, Level 15, State 1, Line 13
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.

我也这样试过:

WITH x as
(SELECT [company name], [appointment call back 1], [appointment call back 2], [appointment date 1], [appointment date 2] FROM Vantrack_Tulsa WHERE [appointment call back 1] BETWEEN '6/1/2016' AND '6/1/2017' 
                              OR [appointment call back 2] BETWEEN '6/1/2016' AND '6/1/2017' 
                              ORDER BY [company name] ASC),
y as
(SELECT [company name], [appointment call back 1], [appointment call back 2], [appointment date 1], [appointment date 2] FROM Vantrack_Tulsa WHERE [appointment date 1] BETWEEN '6/1/2016' AND '6/1/2017'
                              OR [appointment date 2] BETWEEN '6/1/2016' AND '6/1/2017'
                              ORDER BY [company name] ASC)
SELECT * FROM x UNION ALL SELECT * FROM y

但我得到:

Msg 1033, Level 15, State 1, Line 8
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
Msg 156, Level 15, State 1, Line 12
Incorrect syntax near the keyword 'ORDER'.

可能出于同样的原因。

我看到这个问题已经被问过,但假设的解决方案要么从未真正起作用,要么不再起作用。

我检查了一些 SO 答案

有没有我忽略的东西?有什么办法吗?

一种方法是在 SELECT 中为 XY 查询添加另一列来表示您希望它们出现的顺序,并按以下顺序排序该值。

这应该可以满足您的要求:

With x
As (Select [company name],
           [appointment call back 1],
           [appointment call back 2],
           [appointment date 1],
           [appointment date 2],
           1 As Ord
    From   Vantrack_Tulsa
    Where  [appointment call back 1]
           Between '6/1/2016' And '6/1/2017'
           Or [appointment call back 2]
           Between '6/1/2016' And '6/1/2017'
   ),
     y
As (Select [company name],
           [appointment call back 1],
           [appointment call back 2],
           [appointment date 1],
           [appointment date 2],
           2 As Ord
    From   Vantrack_Tulsa
    Where  [appointment date 1]
           Between '6/1/2016' And '6/1/2017'
           Or [appointment date 2]
           Between '6/1/2016' And '6/1/2017'
   )
Select [company name],
       [appointment call back 1],
       [appointment call back 2],
       [appointment date 1],
       [appointment date 2]
From   x
Union All
Select [company name],
       [appointment call back 1],
       [appointment call back 2],
       [appointment date 1],
       [appointment date 2]
From   y
Order By Ord, [company name];

关于如何执行此操作的简要概述:

;WITH CTE_Sets AS (
    SELECT 1 AS set_order, <other columns here>
    FROM Some_Table
    UNION ALL
    SELECT 2 AS set_order, <other columns here>
    FROM Some_Table
)
SELECT <columns>
FROM CTE_Sets
ORDER BY
    set_order,
    CASE set_order
        WHEN 1 THEN <order criteria for set #1>
        WHEN 2 THEN <order criteria for set #2>
    END

需要对UNION ALL的最终结果进行OrderBy,所以把order by子句放在最后。

SELECT * FROM(
(SELECT [company name], [appointment call back 1], [appointment call back 2], [appointment date 1], [appointment date 2] FROM Vantrack_Tulsa WHERE [appointment call back 1] BETWEEN '6/1/2016' AND '6/1/2017' 
                          OR [appointment call back 2] BETWEEN '6/1/2016' AND '6/1/2017') t
UNION ALL

(SELECT [company name], [appointment call back 1], [appointment call back 2], [appointment date 1], [appointment date 2] FROM Vantrack_Tulsa WHERE [appointment date 1] BETWEEN '6/1/2016' AND '6/1/2017'
                          OR [appointment date 2] BETWEEN '6/1/2016' AND '6/1/2017') s
) Q
Order by Q.[company_name] ASC

如果您愿意,也可以使用 ROW_NUMBER()。像这样:

SELECT
  *
FROM
(
  SELECT
    [company name],
    [appointment call back 1],
    [appointment call back 2],
    [appointment date 1],
    [appointment date 2],
    'A' + CAST(ROW_NUMBER() OVER(ORDER BY [company name]) as varchar(50)) as Sequence
  FROM
    Vantrack_Tulsa
  WHERE
    [appointment call back 1] BETWEEN '6/1/2016' AND '6/1/2017' OR
    [appointment call back 2] BETWEEN '6/1/2016' AND '6/1/2017' 

UNION ALL

SELECT
  [company name],
  [appointment call back 1],
  [appointment call back 2],
  [appointment date 1],
  [appointment date 2],
  'B' + CAST(ROW_NUMBER() OVER(ORDER BY [company name]) as varchar(50)) as Sequence
FROM
  Vantrack_Tulsa
WHERE
  [appointment date 1] BETWEEN '6/1/2016' AND '6/1/2017' OR
  [appointment date 2] BETWEEN '6/1/2016' AND '6/1/2017'
) x

ORDER BY
  x.Sequence