使用子查询对房间价格求和时出错

时间:2013-11-12 23:51:45

标签: sql oracle group-by

我试图获得每个酒店在某一天没有预订房间的所有收入损失的总和。

  • 酒店(hotelNo,hotelName,hotelAddress)
  • 房间(hotelNo,roomNo,类型,价格)
  • 访客(guestNo,guestName,guestAddress)
  • 预订(hotelNo,guestNo,dateFrom,dateTo,roomNo)

这是我到目前为止的命令。我觉得这只是一个愚蠢的错误而且我错过了一些简单的东西,但我无法将手指放在上面。

SELECT hotelNo, roomNo, (SUM)price AS "Lost Income"
FROM room r
WHERE roomNo NOT IN
(SELECT roomNo FROM booking b
WHERE to_date('20080706', 'yyyymmdd')
BETWEEN dateFrom AND dateTo
AND r.hotelno = b.hotelno)
ORDER BY hotelNo
GROUP BY hotelNo

1 个答案:

答案 0 :(得分:1)

显而易见的是SUM中的括号错误。另外,ORDER BY应该在GROUP BY之后,您需要以不同的方式转换日期

SELECT hotelNo, SUM(price) AS "Lost Income"
FROM room r
WHERE roomNo NOT IN
(SELECT roomNo FROM booking b
WHERE to_date('20080706', 'yyyymmdd')
BETWEEN dateFrom AND dateTo
AND r.hotelno = b.hotelno)
GROUP BY hotelNo
ORDER BY hotelNo