Sql Query按天和最多人检查可用性

时间:2012-09-24 07:47:49

标签: sql sql-server tsql

你好这是我的db上的2个表: enter image description here

我正在努力让这些酒店有一段时间间隔。 在Rooms表中定义了房间的类型:例如Signe:max_guests = 1,Triple:max_guests = 3 所以对于10人预订我需要类似的东西:

SELECT  SUM(rooms.max_guests * av.RoomAvailable) AS maxPeople 
FROM av AS av INNER JOIN rooms 
ON av.room = rooms.ID AND av.DateofDays BETWEEN '09/28/2012' AND '10/03/2012' 
GROUP BY av.userid
HAVING Count(*) >= 6

但这并不符合我的要求..返回我总max_people可用/用户ID我只需要检查每个用户ID(酒店)每天是否有> = 10(max_people)

感谢您的帮助

更新:如果我使用此查询:

SELECT  av.userid, av.DateofDays, SUM(rooms.max_guests * 

av.RoomAvailable) AS maxPeople 
FROM av AS av INNER JOIN rooms 
ON av.room = rooms.ID AND av.DateofDays BETWEEN '09/28/2012' 

AND '10/03/2012' 
GROUP BY av.userid, av.DateofDays
ORDER BY av.userid, av.DateofDays

我明白了: enter image description here

2 个答案:

答案 0 :(得分:1)

您必须手动输入计数中的天数。

WITH cte AS (
SELECT  av.userid, av.DateofDays, SUM(rooms.max_guests * av.RoomAvailable) AS maxPeople 
FROM av AS av INNER JOIN rooms 
ON av.room = rooms.ID AND av.DateofDays BETWEEN '09/28/2012' AND '10/03/2012' 
GROUP BY av.userid, av.DateofDays
HAVING SUM(rooms.max_guests * av.RoomAvailable) >= 10 
)
select cte.userid 
from cte 
group by av.userid 
having COUNT(*) = 6

答案 1 :(得分:0)

只需将dateofdays添加到SELECT和GROUP BY即可。这将只返回days / hotels> = 10