我正在开发一个在线酒店管理系统项目,该数据库位于mssql中,其表名为BookingDetails,其中列名称为BookingId,BookingFrom和RoomNo。 我需要找出一年内最多天数预订的RoomNo。 我发现预订的客房预订了多少天; 像
select
RoomNo
BookingId,
BookingUpto-BookingFrom
from
BookingDetails;
提供结果预订不同房间的天数,现在我的问题是如何使用所施加的数据i,e具有最小值,最大值或计数功能的天数。 Plz的帮助。 谢谢。
表名BookingDetails
BookingID BookingFrom BookingUpto RoomNo
Id
111 12/22/2013 12:00:00 AM 12/25/2013 12:00:00 AM 101
222 12/23/2013 12:00:00 AM 12/25/2013 12:00:00 AM 210
333 12/24/2013 12:00:00 AM 12/26/2013 12:00:00 AM 305
444 1/4/2014 12:00:00 AM 1/6/2014 12:00:00 AM 101
555 12/28/2013 12:00:00 AM 1/10/2014 12:00:00 AM 305
666 12/29/2013 12:00:00 AM 1/2/2014 12:00:00 AM 210
777 1/20/2014 12:00:00 AM 1/28/2014 12:00:00 AM 305
888 1/29/2014 12:00:00 AM 1/31/2014 12:00:00 AM 101
NULL NULL NULL NULL
答案 0 :(得分:2)
以下列出了每年预订每间客房的天数:
select RoomNo, sum(datediff(day, BookingFrom, BookingUpto)) as daysbooked
from BookingDetails
group by RoomNo;
您可以通过对此列表进行排序来找到预订时间最多的房间:
select RoomNo, sum(datediff(day, BookingFrom, BookingUpto)) as daysbooked
from BookingDetails
group by RoomNo
order by daysbooked desc;
答案 1 :(得分:0)
尝试使用子查询,这将返回特定房间号的最大天数
select t.RoomNo, max(t.days) from (
select RoomNo, BookingId, BookingUpto-BookingFrom days From BookingDetails) t
group by t.RoomNo
答案 2 :(得分:0)
这对我有用: -
SELECT SUM(DateDiff * TotalCount) NumberOfDays,
RoomNo
FROM (SELECT SUM(DATEDIFF(day, BookingFrom, BookingUpTo)) DateDiff,
RoomNo,
count(1) TotalCount
FROM BookingDetails
GROUP BY BookingFrom,BookingUpTo,RoomNo) t
GROUP BY RoomNo
ORDER BY RoomNo
答案 3 :(得分:0)
这应该得到你想要的。该小组每年和每个房间。由此我计算每次预订天数的总和()。因此,如果有人从1月1日到1月4日预订,那应该是3晚预订(4日退房)。然后另一个人从1月4日到1月5日(一晚)预订房间。预订总共4晚。每个预订都会计算IT的住宿时间并将其加起来。
SELECT
YEAR( BookingUpTo ) as Yr,
roomNo,
SUM( datediff( day, BookingUpTo, BookingFrom )) as totalBookedDays
from
BookingDetails
group by
YEAR( BookingUpTo ),
roomNo
order by
totalBookedDays DESC