1

我正在尝试从酒店数据库中回答以下问题

显示 2011 日历年每个月的预订数量。

到目前为止,这是我想出的。

SELECT booking_no,
       date_in,
       Count(*)
FROM   booking
WHERE  date_in >= '01-jan-11'
       AND date_in <= '31-dec-11'
GROUP  BY booking_no,
          date_in
ORDER  BY date_in;

这些是数据库标题

来宾

Name Type
--------------------- ------------
GUEST_NO 
SURNAME 
GIVEN 
DOB 
SEX 
HOME_PHONE 
WORK_PHONE 
FAX 
ADDRESS 
SUBURB 
STATE 
POSTCODE 

房间

Name Type
--------------------- ------------
ROOM_NO 
FLOOR_NO 
SPA_BATH 
NUM_BEDS 
PRICE 
LINKED_ROOM_NO )

预订

Name Type
--------------------- -----------
BOOKING_NO 
GUEST_NO 
ROOM_NO
DATE_IN 
DATE_OUT 
4

3 回答 3

1

你需要的是一个MONTH函数。这在大多数数据库引擎中都是一样的。所以试试

SELECT booking_no, Month(date_in), Count(booking_no)

GROUP BY Month(date_in)
ORDER BY Month(date_in);

您还可以使用以下命令简化 where 子句YEAR

WHERE Year(date_in) = 2011

共:

SELECT Month(date_in),
       Count(*)
FROM   booking
WHERE  Year(date_in) = 2011
GROUP  BY Month(date_in)
ORDER  BY Month(date_in);
于 2012-08-29T09:18:28.600 回答
1

你想要每月的数量,所以

select month(date_in), count(*) 
from booking 
where year(date_in)=2011
group by month(date_in)
order by month(date_in); 
于 2012-08-29T09:14:48.687 回答
1

试试他的:

SELECT Count(booking_no) bookings,
       Month(date_in)    month_name
FROM   booking
WHERE  date_in >= '01-jan-11'
       AND date_in <= '31-dec-11'
GROUP  BY Month(date_in)
ORDER  BY Month(date_in);

不要在 where 子句中使用“ ”,如果您在列year(date_in)=2011上有索引,它将阻止使用索引。date_in如果你没有,那也没关系。

于 2012-08-29T09:17:53.607 回答