如何将单独的SQL查询,每个ORDER BYs合并到一个查询中?

时间:2017-06-01 19:28:48

标签: sql sql-server sql-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' 
                          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答案

我有什么东西可以俯瞰吗?有什么办法吗?

4 个答案:

答案 0 :(得分:2)

执行此操作的方法是在SELECTX查询的Y中添加另一列,以表示您希望它们出现的顺序,并按该值排序

这应该做你想要的:

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];

答案 1 :(得分:0)

快速概述如何执行此操作:

;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

答案 2 :(得分:0)

你需要OrderBy UNION ALL的最终结果,所以最后放置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

答案 3 :(得分:0)

如果您愿意,也可以使用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