简体   繁体   English

在2张桌子上选择UNION ALL,并选择Cases

[英]UNION ALL on 2 tables select with Cases

I'm running SQL Server 2005. I have 2 tables with the same columns but holding very different data. 我正在运行SQL Server2005。我有2个表,这些表具有相同的列,但保存的数据却非常不同。

SELECT *
FROM Table1 
WHERE ItemID IN ('4','2','1') 
ORDER BY 
    CASE  WHEN ItemID = 4 then 1 WHEN ItemID = 2 then 2 WHEN ItemID = 1 then 3 END 
UNION ALL 
SELECT * 
FROM Table2 
WHERE ItemID IN ('3','1','5','2') 
ORDER BY 
    CASE  WHEN ItemID = 3 then 4 WHEN ItemID = 1 then 5 WHEN ItemID = 5 then 6 WHEN ItemID = 2 then 7 END

I need to keep the order of the ItemID in the order that they are selected which is why I used CASE . 我需要按照选择它们的顺序保持ItemID的顺序,这就是为什么我使用CASE This all works fine on each table but I can't find a way to combine them into 1 table of results with the results of each table ordered. 所有这些在每个表上都可以正常工作,但是我找不到一种将它们组合成1个结果表和每个表的排序结果的方法。

ie

4 (Table1)
2 (Table1)
1 (Table1)
3 (Table2)
1 (Table2)
5 (Table2)
2 (Table2)

Extremely grateful for any and all help. 非常感谢您提供的所有帮助。

Try this: 尝试这个:

SELECT *
FROM (
    SELECT * , 1 as ord
    FROM Table1 
    WHERE ItemID IN (4, 2, 1) 
  UNION ALL 
    SELECT * , 2 as ord
    FROM Table2 
    WHERE ItemID IN (3, 1, 5, 2) ) t
ORDER BY  
    ord,
    CASE  WHEN ItemID = 3 then 4 WHEN ItemID = 1 then 5 WHEN ItemID = 5 then 6 WHEN ItemID = 2 then 7 END,
    CASE  WHEN ItemID = 4 then 1 WHEN ItemID = 2 then 2 WHEN ItemID = 1 then 3 END 

Or 要么

SELECT *,
    ROW_NUMBER() OVER (ORDER BY ord,
        CASE  WHEN ItemID = 3 then 4 WHEN ItemID = 1 then 5 WHEN ItemID = 5 then 6 WHEN ItemID = 2 then 7 END,
        CASE  WHEN ItemID = 4 then 1 WHEN ItemID = 2 then 2 WHEN ItemID = 1 then 3 END) as RowNo 

FROM (
    SELECT * , 1 as ord
    FROM Table1 
    WHERE ItemID IN (4, 2, 1) 
  UNION ALL 
    SELECT * , 2 as ord
    FROM Table2 
    WHERE ItemID IN (3, 1, 5, 2) ) t

You can use the following query: 您可以使用以下查询:

SELECT Table1.* , x.[Order] AS Ord
FROM Table1 
CROSS APPLY (SELECT CASE ItemID
                       WHEN 4 THEN 1 
                       WHEN 2 THEN 2 
                       WHEN 1 THEN 3 
             END) x([Order])  
WHERE ItemID IN ('4','2','1') 

UNION ALL 

SELECT Table2.* , y.[Order] AS Ord
FROM Table2 
CROSS APPLY (SELECT CASE ItemID 
                       WHEN 3 THEN 4 
                       WHEN 1 THEN 5 
                       WHEN 5 THEN 6 
                       WHEN 2 THEN 7 
                    END) y([Order])
WHERE ItemID IN ('3','1','5','2')
ORDER BY Ord

Calculated field [Order] guarantees that records from Table1 will come first, followed by records of Table2 . 计算字段[Order]确保来自Table1记录将首先出现,然后是Table2的记录。 It also ensures ordering within each Table1 or Table2 partition. 它还确保在每个Table1Table2分区内排序。

Demo here 在这里演示

This is an alternative syntax without the use of CROSS APPLY : 这是不使用CROSS APPLY的另一种语法:

SELECT Table1.*, 
       CASE ItemID
          WHEN 4 THEN 1 
          WHEN 2 THEN 2 
          WHEN 1 THEN 3 
       END AS Ord
FROM Table1 
WHERE ItemID IN ('4','2','1') 

UNION ALL 

SELECT Table2.*, 
       CASE ItemID 
          WHEN 3 THEN 4 
          WHEN 1 THEN 5 
          WHEN 5 THEN 6 
          WHEN 2 THEN 7 
       END AS Ord
FROM Table2 
WHERE ItemID IN ('3','1','5','2')
ORDER BY Ord

Demo here 在这里演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM