我试图从酒店数据库回答以下问题
显示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 )
BOOKING
Name Type
--------------------- -----------
BOOKING_NO
GUEST_NO
ROOM_NO
DATE_IN
DATE_OUT
答案 0 :(得分:1)
您想要每月的号码,所以
select month(date_in), count(*)
from booking
where year(date_in)=2011
group by month(date_in)
order by month(date_in);
答案 1 :(得分: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
列上有索引,则会阻止使用索引。如果你没有,那没关系。
答案 2 :(得分:1)
您需要的是MONTH
功能。这在大多数数据库引擎中都是相同的。所以试试
SELECT booking_no, Month(date_in), Count(booking_no)
和
GROUP BY Month(date_in)
ORDER BY Month(date_in);
您还可以使用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);