如何在查询中的查询中使用 ISNULL?

发布于 2024-12-11 00:46:06 字数 3048 浏览 0 评论 0原文

下面的 SQL 查询现在可以正确返回 RoomsAvailable 字段,除非有 0 个房间被占用,其中 SQL 命令用 NULL 减去 number_of_rooms 并将 NULL 输出到列。我尝试了许多 ISNULL 变体,发现它不起作用;有人知道我应该怎么做吗?

SQL:

SELECT
    Hotel_2.hotel_code, 
    Hotel_2.hotel_country, 
    Room_type_rates_2.room_type_code, 
    Room_type_rates_2.number_of_rooms, 
    Types_2.room_type, 
    Room_type_rates_2.rates, 
            Room_type_rates_2.number_of_rooms -
            (SELECT 
                DISTINCT (SELECT
                             COUNT(dbo.Hotel.hotel_code) AS RoomsTake
                          FROM
                              dbo.Hotel 
                          INNER JOIN dbo.Hotel_Reservation 
                              ON dbo.Hotel.hotel_code = dbo.Hotel_Reservation.hotel_code 
                          INNER JOIN dbo.Room_type_rates 
                              ON dbo.Hotel.hotel_code = dbo.Room_type_rates.hotel_code 
                          INNER JOIN dbo.Types 
                              ON dbo.Hotel_Reservation.room_type_code = dbo.Types.room_type_code 
                              AND dbo.Room_type_rates.room_type_code = dbo.Types.room_type_code
                          WHERE
                              (dbo.Room_type_rates.room_type_code = Room_type_rates_1.room_type_code) 
                              AND (dbo.Hotel.hotel_code = Hotel_1.hotel_code)
                              AND (dbo.Hotel_Reservation.checkin_date >= Hotel_Reservation_1.checkin_date)
                              AND (dbo.Hotel_Reservation.checkout_date <= Hotel_Reservation_1.checkout_date)
                          ) AS RoomsTaken
               FROM
                   dbo.Hotel AS Hotel_1 
               INNER JOIN dbo.Hotel_Reservation AS Hotel_Reservation_1
                   ON Hotel_1.hotel_code = Hotel_Reservation_1.hotel_code 
               INNER JOIN dbo.Room_type_rates AS Room_type_rates_1 
                   ON Hotel_1.hotel_code = Room_type_rates_1.hotel_code 
               INNER JOIN dbo.Types AS Types_1 
                   ON Hotel_Reservation_1.room_type_code = Types_1.room_type_code 
                   AND Room_type_rates_1.room_type_code = Types_1.room_type_code
               WHERE
                   (Hotel_Reservation_1.checkin_date >= '11/19/2011') 
                   AND (Hotel_Reservation_1.checkout_date <= '12/01/2011') 
                   AND (Hotel_1.hotel_country = 'Adelaide') 
                   AND (Types_1.room_type_code = Types_2.room_type_code)
               ) AS RoomsAvailable 
FROM 
    dbo.Hotel AS Hotel_2 
INNER JOIN dbo.Room_type_rates AS Room_type_rates_2 
    ON Hotel_2.hotel_code = Room_type_rates_2.hotel_code 
INNER JOIN dbo.Types AS Types_2 
    ON Room_type_rates_2.room_type_code = Types_2.room_type_code

当前输出:

<代码> ADL20 阿德莱德 CPL 6 情侣套房 514.0000 3

ADL20   Adelaide    FYU         3   Family Suite    533.0000    2

ADL20   Adelaide    KNG         2   King's Bedroom  556.0000    NULL

My SQL Query below is now Returning the RoomsAvailable field correctly except when theres 0 rooms taken, in which the SQL command subtracts number_of_rooms with NULL and outputs NULL to the Column. I have tried numerous ISNULL variations and found that it doesnt work; anyone know how I should be doing this?

SQL :

SELECT
    Hotel_2.hotel_code, 
    Hotel_2.hotel_country, 
    Room_type_rates_2.room_type_code, 
    Room_type_rates_2.number_of_rooms, 
    Types_2.room_type, 
    Room_type_rates_2.rates, 
            Room_type_rates_2.number_of_rooms -
            (SELECT 
                DISTINCT (SELECT
                             COUNT(dbo.Hotel.hotel_code) AS RoomsTake
                          FROM
                              dbo.Hotel 
                          INNER JOIN dbo.Hotel_Reservation 
                              ON dbo.Hotel.hotel_code = dbo.Hotel_Reservation.hotel_code 
                          INNER JOIN dbo.Room_type_rates 
                              ON dbo.Hotel.hotel_code = dbo.Room_type_rates.hotel_code 
                          INNER JOIN dbo.Types 
                              ON dbo.Hotel_Reservation.room_type_code = dbo.Types.room_type_code 
                              AND dbo.Room_type_rates.room_type_code = dbo.Types.room_type_code
                          WHERE
                              (dbo.Room_type_rates.room_type_code = Room_type_rates_1.room_type_code) 
                              AND (dbo.Hotel.hotel_code = Hotel_1.hotel_code)
                              AND (dbo.Hotel_Reservation.checkin_date >= Hotel_Reservation_1.checkin_date)
                              AND (dbo.Hotel_Reservation.checkout_date <= Hotel_Reservation_1.checkout_date)
                          ) AS RoomsTaken
               FROM
                   dbo.Hotel AS Hotel_1 
               INNER JOIN dbo.Hotel_Reservation AS Hotel_Reservation_1
                   ON Hotel_1.hotel_code = Hotel_Reservation_1.hotel_code 
               INNER JOIN dbo.Room_type_rates AS Room_type_rates_1 
                   ON Hotel_1.hotel_code = Room_type_rates_1.hotel_code 
               INNER JOIN dbo.Types AS Types_1 
                   ON Hotel_Reservation_1.room_type_code = Types_1.room_type_code 
                   AND Room_type_rates_1.room_type_code = Types_1.room_type_code
               WHERE
                   (Hotel_Reservation_1.checkin_date >= '11/19/2011') 
                   AND (Hotel_Reservation_1.checkout_date <= '12/01/2011') 
                   AND (Hotel_1.hotel_country = 'Adelaide') 
                   AND (Types_1.room_type_code = Types_2.room_type_code)
               ) AS RoomsAvailable 
FROM 
    dbo.Hotel AS Hotel_2 
INNER JOIN dbo.Room_type_rates AS Room_type_rates_2 
    ON Hotel_2.hotel_code = Room_type_rates_2.hotel_code 
INNER JOIN dbo.Types AS Types_2 
    ON Room_type_rates_2.room_type_code = Types_2.room_type_code

Current Output :


ADL20 Adelaide CPL 6 Couple Suite 514.0000 3

ADL20   Adelaide    FYU         3   Family Suite    533.0000    2

ADL20   Adelaide    KNG         2   King's Bedroom  556.0000    NULL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伏妖词 2024-12-18 00:46:06

你的意思是这个吗?:

...ISNULL(COUNT(dbo.Hotel.*),0)...

或者你根本就得到了NULL?在这种情况下,我怀疑您的 JOIN 可能有问题。

您可以将 ISNULL 包裹在任何嵌套子查询或值周围。

Do you mean this?:

...ISNULL(COUNT(dbo.Hotel.*),0)...

Or is the problem that you're getting NULL at all? In that case I would suspect your JOINs may have a problem.

You can wrap an ISNULL around any nested subquery or value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文