2017-06-01 88 views
0

理论上,我希望基本上创建两个具有相同列的独立表格,并根据需要分别对它们分别排序,然后简单地将其放在另一个表格下面并保留该顺序。如何合并单独的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'. 

也许出于同样的原因。

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

一些所谓的答案我经过

有我俯瞰的东西吗?任何方式来做到这一点?

+0

你说“表”,但你的错误信息意味着非常不同的东西。那么 - 你的目标是什么** EXACTLY **?视图像表格一样没有固有的顺序 - 您必须使用顺序依据子句来提供顺序(一般而言,这在视图中不起作用)。 – SMor

回答

2

要做到这一点,方法是在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]; 
+0

啊......当然,这会做伎俩,是吧?好的电话,谢谢你! – VoidKing

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 
0

您需要将所有排序依据联盟的最终结果,所以下订单由最后条款。

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