计算月收入损失

时间:2013-02-26 08:24:17

标签: mysql sql

我有3张桌子:

Hotel {hotelID, hotelName, city}
Room {hotelID, roomNo, price, type}
Booking {hotelID, roomNo, startDate, endDate}

我想找到当月损失的收入(即计算当月每天的收入损失,然后计算总收入)

我如何做到这一点?

快速编辑:只需要查找hotelName ='Radisson'的收入

2 个答案:

答案 0 :(得分:2)

应该工作:

SELECT COALESCE(SUM(r.price), 0) 
    FROM Room AS r INNER JOIN Booking AS b ON r.hotelID = b.hotelID AND r.roomNo = b.roomNo 
    INNER JOIN Hotel AS h ON r.hotelID = h.hotelID 
    WHERE (month(b.startDate) > month(current_date()) OR month(b.endDate) < month(current_date()))
    AND h.hotelName = 'Radisson';

答案 1 :(得分:0)

这是您的查询的开始。你懒得放数据,所以我懒得去查询mysql的查询形式......

查看每个房间数据后,如果您愿意,可以合并为一个总和。这就是你编写查询以确保每一步都正确的方法

select
  ht.hotelid
  ,ht.hotelname
  ,ht.city
  ,rm.roomno
  ,rm.price
  ,rm.type
  ,30 as days -- use your datediff function for month instead of literal 30
  ,30 * rm.price as potential -- use your datediff function instead of literal 30
  ,sum(datediff(day, startdate, enddate)) * rm.price as earned
  --, potential - earned as lost (use the 2 prior columns to calc)      
from hotel ht
join room rm on ht.hotelid = rm.hotelid
left join booking bk on bk.hotelid = rm.hotelid 
   and bk.roomno =bk.roomno