Finding AVG of COUNT Groupings
562119Feb 15 2007 — edited Feb 15 2007Hi,
I'm new to this forum and have what a general question about the GROUP BY clause.
Given the following table:
TABLENAME: Hotel
HOTELNO GUESTNO DATEFROM DATETO ROOMNO
-------- ---------- --------- --------- ----------
1 1 09-FEB-07 10-FEB-07
1 2 01-AUG-07 08-AUG-07
4 3 12-FEB-07 13-FEB-07 83
4 2 13-FEB-07 14-FEB-07 25
4 4 13-FEB-07 14-FEB-07 83
4 3 27-AUG-07 29-AUG-07 25
3 3 27-AUG-07 29-AUG-07 30
2 3 27-AUG-07 29-AUG-07 22
2 3 25-AUG-07 26-AUG-07 22
I want to find the average number of bookings that occur in AUG for each hotel. Therefore, the answer for this would be something like:
AverageNumAugBookForEachHotel
=========================
1.25
So, the idea I believe is to first group the total number of AUGUST bookings per hotel as follows:
SELECT COUNT(hotelNo) FROM Booking WHERE dateFrom LIKE '%AUG%' GROUP BY hotelNo
The result of this is:
SQL> SELECT COUNT(hotelNo) FROM Booking WHERE dateFrom LIKE '%AUG%' GROUP BY hotelNo;
COUNT(HOTELNO)
--------------
1
2
1
1
Now, since I want the average of this, wouldn't I do something like this?:
SELECT AVG(SELECT COUNT(hotelNo) FROM Booking WHERE dateFrom LIKE '%AUG%' GROUP BY hotelNo) FROM Booking;
However, unfortunately, I get the following error message:
SELECT AVG(SELECT COUNT(hotelNo) FROM Booking WHERE dateFrom LIKE '%AUG%' GROUP BY hotelNo)
*
ERROR at line 1:
ORA-00936: missing expression
Any idea how do this?
Thank you,
Randy