0

I'm trying to count the number of sessions in each room and each building. As of now I've written this query:

SELECT COUNT(BUILDINGNO)*COUNT(ROOMNO), BUILDINGNO, ROOMNO
FROM ROOM
GROUP BY BUILDINGNO
ORDER BY ROOMNO;

but I am getting an error saying it is missing a group by expression.

1
  • try this: SELECT COUNT(BUILDINGNO)*COUNT(ROOMNO), BUILDINGNO, ROOMNO FROM ROOM GROUP BY BUILDINGNO,ROOMNO ORDER BY ROOMNO; Commented Oct 6, 2017 at 5:04

3 Answers 3

4

I think you should be aggregating by building and room. Also, you omitted a table which records attendance based on room and building. Your query should look something like this:

SELECT
    r.BUILDINGNO,
    r.ROOMNO,
    COUNT(cs.SPEAKERID) AS cnt
FROM ROOM r
LEFT JOIN CONFERENCESESSION cs
    ON r.BUILDINGNO = cs.BUILDINGNO AND
       r.ROOMNO = cs.ROOMNO
GROUP BY
    r.BUILDINGNO,
    r.ROOMNO
ORDER BY
    r.BUILDINGNO,
    r.ROOMNO;

It is important to do a left join between the ROOM and CONFERENCESESSION tables because perhaps a given room have no sessions whatsoever. But we would still want to report a total of zero for that room.

Sign up to request clarification or add additional context in comments.

Comments

1

This error is happening because there is a field in your SELECT clause that is not in your GROUP BY clause or being aggregated.

Also, using the conferencesession table and joining on the BUILDINGNO and ROOMNO fields will let you see the number of sessions.

Try this query:

SELECT 
r.BUILDINGNO,
r.ROOMNO,
COUNT(s.SESSIONID) AS session_count
FROM room r
LEFT OUTER JOIN conferencesession s ON r.BUILDINGNO = s.BUILDINGNO AND r.ROOMNO = s.ROOMNO
GROUP BY r.BUILDINGNO, r.ROOMNO
ORDER BY r.BUILDINGNO, r.ROOMNO;

You can change the ORDER BY to order on any of these columns, even something like ORDER BY COUNT(*) will work.

Also, as Tim Biegeleisen has mentioned, using a LEFT OUTER JOIN will give you all room records that have no session. If you use an INNER JOIN, then any rooms with no sessions will not be shown.

9 Comments

Thanks, how do i get it to count the number of sessions in building B1 and room R1? i thought i got it in my original query but it this just does 1x1 for every COUNT statement
This query should show a record where buildingno = B1 and room = R1, and a corresponding COUNT value. Does it show that?
It does, but I need a query that counts the amount of sessions in building b1 and r1. This query displays the sessions, all with a count of 1.
How do you define a "session"? This query counts records in the "room" table. Is a "session" another table?
@TimothyBolton I think you left out a table.
|
1

I do not understand the logic you are going for, but you are getting this particular error because you have not specified all of your non-aggregation function fields in the GROUP BY clause. In other words, try modifying it to this:

SELECT COUNT(BUILDINGNO) * COUNT(ROOMNO), BUILDINGNO, ROOMNO
FROM ROOM
GROUP BY BUILDINGNO, ROOMNO
ORDER BY ROOMNO;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.