2

Ok I can't figure this one out I have

select job "Some job", Count(a.bossID) "Number Of bosses" 
from boss a, places b, place_boss ba 
where ba.bossid = a.bossid and ba.placeid = b.placeid 
   and Count(a.bossID)>1 
group by job;

I get Group function not allowed here I need make sure that if the job has one boss it doesn't show.

Any Idea what I'm messing up here?

5 Answers 5

7

You need to use having. The Where clause cannot contain aggregates. Additionally I have changed it to use the explicit JOIN syntax as this is (IMO) clearer and less likely to cause inadvertent Cartesian joins.

select job             "Some job",
       Count(a.bossID) "Number Of bosses"
from   boss a
       join place_boss ba
         on ba.bossid = a.bossid
       join places b
         on ba.placeid = b.placeid
group  by job
having Count(a.bossID) > 1;  
Sign up to request clarification or add additional context in comments.

Comments

3

where clauses are applied at the row level, literally a "should this row be included in the result set". At the time that decision is being made, it's impossible for the count() function to have finished its work, as not all rows have been counted yet.

That's where having clauses come in to play. They're exactly like where clauses, but applied immediately before the result set gets sent to the client, after the aggregate functions have completed their work.

Technically, doing

SELECT * FROM table HAVING somefield='a'
SELECT * FROM table WHERE somefield='a'

are identical, except the having clause will entail a bit more work on the part of the server because all the rows are fetched, THEN filtered, rather than being filtered before fetching.

Comments

1
select 
    job "Some job", 
    Count(a.bossID) "Number Of bosses"  
from 
    boss a, places b, place_boss ba  
where 
    ba.bossid = a.bossid and ba.placeid = b.placeid  
group by job
having Count(a.bossID) > 1; 

Comments

1

I would try it with a HAVING clause:

select job "Some job", Count(a.bossID) "Number Of bosses" 
from boss a, places b, place_boss ba 
where ba.bossid = a.bossid 
group by job
having Count(a.bossID)>1 

Comments

0

Never mind it should be

select job "Some job", Count(a.bossID) "Number Of bosses" from boss a, places b, place_boss ba where ba.bossid = a.bossid and ba.placeid = b.placeid group by job having Count(a.bossID)>1;

3 Comments

Opps already 3 answers, holy crap. I'll accept Martins when I can!
"Nevermind"? I'd accept an answer. Reminds of that joke about the Irishman who was looking for a parking spot without success. He swore that if God would give him a parking space he'd give up the whiskey and go back to church every Sunday. Just as he finished his prayer a space miraculously opened before him. "Nevermind," he said, "I found one!"
I was going to :P I posted my answer without refreshing hence the "I'll accept Martins when I can!" :)

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.