So here’s my question (i’m using Oracle SQL by the way)
I have 3 result queries from my database (one is total number of hotels, one is number of hotels being constructed and one is number of hotels completed).
How do you join all three queries, so that i get 1 row, and 3 distinct columns? I’ve tried using UNION, but all that gets me is 1 column, but 3 rows.
My code looks like this:
(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));
And the resulting output looks like this:
TOTALHOTELS
-----------
2
8
10
I need it so that there’s only one row, and 3 columns.
Any help?
The general answer, which also works when your
SELECTresults have more than one column, is:although in this case, you don’t need the union/cross join: