首页 > 解决方案 > 如何获取客户数量?

问题描述


我需要一些有关 SQL 查询的帮助。
假设我们有一个管理酒店预订的示例数据库:

Customer(ID, name, birthDate, city);
Room(number, floor, beds, roomtype, price);
Bookings(Customer, roomNumber, floor, checkInDate, checkOutDate);

我需要知道哪些客户只预订了经济型客房。这是我的查询:

select Customer from Bookings
   join Room on(Bookings.num = camera.roomNumber and Bookings.floor= 
   Room.floor)
        where (Bookings.Customer, Bookings.floor) not in (select number, floor from 
        Room where roomType != 'economic')

我的问题是这个查询向我显示了预订经济房的客户,但它也向我显示了预订其他类型房间的客户。如何限制输出以获取仅预订经济房的客户?谢谢

标签: sqlpostgresql

解决方案


您可以使用聚合:

select b.Customer
from Bookings b join
     Room r
     on b.num = r.roomNumber and b.floor = r.floor
group by b.Customer
having min(roomType) = max(roomType) and min(roomType) = 'economic';

推荐阅读