Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Finding AVG of COUNT Groupings

562119Feb 15 2007 — edited Feb 15 2007
Hi,
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 15 2007
Added on Feb 15 2007
7 comments
890 views