可以使用UNION将3个结果查询连接在一起,但保持列不同吗?

时间:2012-01-31 07:11:21

标签: sql oracle union

所以这是我的问题(我顺便使用Oracle SQL)

我的数据库有3个结果查询(一个是酒店总数,一个是正在建设的酒店数量,一个是已完成的酒店数量)。

如何加入所有三个查询,以便获得1行和3个不同的列?我尝试过使用UNION,但所有得到的都是1列,但是有3行。

我的代码如下所示:

 (SELECT COUNT(Hotel.hotelName) AS TotalHotels FROM Hotel)
 UNION
 (SELECT COUNT(Hotel.hotelName) AS NumConstructing FROM Hotel
 WHERE NOT EXISTS
 (SELECT Room.* FROM Room WHERE Room.hotelNo = Hotel.hotelNo))
 UNION
 (SELECT COUNT(Hotel.hotelName) AS NumCompleted FROM Hotel
 WHERE EXISTS
 (SELECT Room.* FROM Room WHERE Room.hotelNo = Hotel.hotelNo));

结果输出如下:

TOTALHOTELS
-----------
  2
  8
 10

我需要它,因此只有一行和3列。

任何帮助?

4 个答案:

答案 0 :(得分:3)

当您的SELECT结果包含多列时,一般答案也适用:

SELECT TotalHotels, NumConstructing, NumCompleted
FROM
    (SELECT COUNT(Hotel.hotelName) AS TotalHotels FROM Hotel)
    AS TotalHotels
CROSS JOIN 
    (SELECT COUNT(Hotel.hotelName) AS NumConstructing FROM Hotel
     WHERE NOT EXISTS
     (SELECT Room.* FROM Room WHERE Room.hotelNo = Hotel.hotelNo))
    AS NumConstructing
CROSS JOIN
    (SELECT COUNT(Hotel.hotelName) AS NumCompleted FROM Hotel
     WHERE EXISTS
     (SELECT Room.* FROM Room WHERE Room.hotelNo = Hotel.hotelNo))
    AS NumCompleted;

虽然在这种情况下,您不需要联合/交叉联接:

SELECT
    COUNT(1) AS TotalHotels,
    COUNT(CASE WHEN NOT EXISTS
     (SELECT 1 FROM Room WHERE Room.hotelNo = Hotel.hotelNo) THEN 1 END) AS NumConstructing,
    COUNT(CASE WHEN EXISTS
     (SELECT 1 FROM Room WHERE Room.hotelNo = Hotel.hotelNo) THEN 1 END) AS NumCompleted
FROM
    Hotel;

答案 1 :(得分:1)

在真正基本的转换中:

select 
(SELECT COUNT(Hotel.hotelName) FROM Hotel) AS TotalHotels 
, (SELECT COUNT(Hotel.hotelName) FROM Hotel
   WHERE NOT EXISTS
   (SELECT Room.* FROM Room WHERE Room.hotelNo = Hotel.hotelNo)) AS NumConstructing 
, (SELECT COUNT(Hotel.hotelName) FROM Hotel
   WHERE EXISTS
   (SELECT Room.* FROM Room WHERE Room.hotelNo = Hotel.hotelNo)) AS NumCompleted
from dual;

答案 2 :(得分:0)

SELECT 
 (SELECT COUNT(Hotel.hotelName) FROM Hotel) AS TotalHotels,
 (SELECT COUNT(Hotel.hotelName) FROM Hotel WHERE NOT EXISTS (SELECT Room.* FROM Room WHERE Room.hotelNo = Hotel.hotelNo)) AS NumConstructing,
 (SELECT COUNT(Hotel.hotelName) FROM Hotel WHERE EXISTS (SELECT Room.* FROM Room WHERE Room.hotelNo = Hotel.hotelNo)) AS NumCompleted

答案 3 :(得分:0)

通过使用子查询和双表来完成:

SELECT (SELECT COUNT(Hotel.hotelName) FROM Hotel) AS TotalHotels,
       (SELECT COUNT(Hotel.hotelName) 
          FROM Hotel
         WHERE NOT EXISTS (SELECT Room.* 
                             FROM Room 
                            WHERE Room.hotelNo = Hotel.hotelNo)) AS NumConstructing,
       (SELECT COUNT(Hotel.hotelName) AS NumCompleted  
          FROM Hotel
         WHERE EXISTS (SELECT Room.* 
                         FROM Room 
                        WHERE Room.hotelNo = Hotel.hotelNo))
  FROM dual;

但是为了表现,我会使用类似的东西(对不起可能的拼写错误):

SELECT Count(DISTINCT h.hotelNo) TotalHotels,
       Sum(Decode(r.hotelNo,
                  NULL, 1,
                  0) NumConstructing,
       Sum(Decode(r.hotelNo,
                  NULL, 0,
                  1) NumCompleted
  FROM Hotel h,
       Room  r
 WHERE h.hotelNo = r.hotelNo (+)