I am currently having an issue with the GROUP BY statements in Oracle Database 10g with my database.
I have four tables in this database – Advertisements, Staff, StaffGrade and StaffOnAd. What I’m trying to query is for advertisements with more than three staff members working on them, I want to list that advert title and the number of staff members who have a payment grade greater than 2.
Below is my attempt:
SELECT Camp.Title
FROM Campaign Camp
LEFT JOIN WorksOn Wo ON Camp.Title = Wo.Title
LEFT JOIN Staff Stf ON Wo.StaffNo = Stf.StaffNo
LEFT JOIN StaffOnGrade StfOGrde ON Stf.StaffNo = StfOGrde.StaffNo
WHERE StfOGrde.Grade > 2 AND Camp.Title IN (SELECT Camp2.Title FROM Campaign Camp2
LEFT JOIN WorksOn Wo2 ON Camp2.Title = Wo2.Title
WHERE COUNT(Camp2.Title) > 3)
GROUP BY Camp.Title
The error I received with the above is: ORA-00934: group function is not allowed here
I searched around and basically that error tells me that the way I’m grouping is wrong but I have no idea why that is. Also I was just wondering was is the correct way to format the above code?
Any help is appreciated.
NOTE: Just some more information regarding the tables below
- Advertisements basically has fields regarding air date etc.
- Staff has their personal details recorded.
- StaffGrade shows what payment grade a staff member is on.
- StaffOnAd shows what staff member works on what advertisement by the
title of the ad.
EDIT 1: Tried the following after comments:
SELECT Camp.Title
FROM Campaign Camp
LEFT JOIN WorksOn Wo ON Camp.Title = Wo.Title
LEFT JOIN Staff Stf ON Wo.StaffNo = Stf.StaffNo
LEFT JOIN StaffOnGrade StfOGrde ON Stf.StaffNo = StfOGrde.StaffNo
WHERE StfOGrde.Grade > 2 AND Camp.Title IN (SELECT Camp2.Title FROM Campaign Camp2
LEFT JOIN WorksOn Wo2 ON Camp2.Title = Wo2.Title
HAVING COUNT(Camp2.Title) > 3)
GROUP BY Camp.Title
However, I received a new error: ORA-00937: not a single-group group function
EDIT 2:
SELECT Camp.Title
FROM Campaign Camp
LEFT JOIN WorksOn Wo ON Camp.Title = Wo.Title
LEFT JOIN Staff Stf ON Wo.StaffNo = Stf.StaffNo
LEFT JOIN StaffOnGrade StfOGrde ON Stf.StaffNo = StfOGrde.StaffNo
WHERE StfOGrde.Grade > 2
AND Camp.Title IN
( SELECT Camp2.Title
FROM Campaign Camp2
LEFT JOIN WorksOn Wo2
ON Camp2.Title = Wo2.Title
GROUP BY Camp2.Title
HAVING COUNT(Camp2.Title) > 3
)
GROUP BY Camp.Title
No errors in this revised query. However, the only results that are being displayed is titles of the advertisement where I also need the amount of staff members working on that advert with grade payment greater than 2. I just assumed that the last group by statement would fix this issue, but I don’t think its necessary.
First of all… Always, when you put into your select some condition in the left joined table, you’ll have an inner join.
Based in your requirements:
I would do something like this: