If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > HELP!! - SQL Statement does not work!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-30-03, 08:28
coolowen coolowen is offline
Registered User
 
Join Date: Jan 2003
Posts: 56
HELP!! - SQL Statement does not work!

select d.dname, count(s.staffid) scount
from tstaff s, tdept d
where s.deptid = d.deptid
group by d.dname
having scount > (select avg(count(s.staffid))
from tstaff s
group by s.staffid)
;

can anyone tell me why the above statement does not run. I am getting the following errors:

ERROR at line 5:
ORA-00904: invalid column name

I am trying to get the name of the department and the number of staff who have a higher than average number of staff assigned to that department.... any suggestions!?
Reply With Quote
  #2 (permalink)  
Old 04-30-03, 08:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
scount is a column alias so you might want to put the actual expression into the HAVING clause

however, there's still a problem

the subquery in the HAVING clause is not scalar, i.e. it can return more than one value


rudy
Reply With Quote
  #3 (permalink)  
Old 04-30-03, 08:43
coolowen coolowen is offline
Registered User
 
Join Date: Jan 2003
Posts: 56
Quote:
Originally posted by r937
scount is a column alias so you might want to put the actual expression into the HAVING clause

however, there's still a problem

the subquery in the HAVING clause is not scalar, i.e. it can return more than one value


rudy
Thanks! I will try that.
Reply With Quote
  #4 (permalink)  
Old 04-30-03, 09:52
coolowen coolowen is offline
Registered User
 
Join Date: Jan 2003
Posts: 56
Quote:
Originally posted by r937
scount is a column alias so you might want to put the actual expression into the HAVING clause

however, there's still a problem

the subquery in the HAVING clause is not scalar, i.e. it can return more than one value


rudy
Hi,

I tried what you suggested but I am still getting problems. Is there an easier way of comparing the number of staff in each department to the overall average across all departments?!

C.
Reply With Quote
  #5 (permalink)  
Old 04-30-03, 10:04
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
the error probably wasn't the alias, then -- like i said, your subquery wasn't scalar
Code:
select d.dname, count(s.staffid) scount
from tstaff s, tdept d
where s.deptid = d.deptid
group by d.dname
having count(s.staffid) > 
  ( select avg(deptcount)
      from ( select d.deptid, count(*) as deptcount
                      from tstaff s, tdept d
                    where s.deptid = d.deptid
                  group by d.deptid ) as deptcounts
  )
caution: untested

Last edited by r937; 04-30-03 at 10:16.
Reply With Quote
  #6 (permalink)  
Old 04-30-03, 10:24
coolowen coolowen is offline
Registered User
 
Join Date: Jan 2003
Posts: 56
Quote:
Originally posted by r937
the error probably wasn't the alias, then -- like i said, your subquery wasn't scalar
Code:
select d.dname, count(s.staffid) scount
from tstaff s, tdept d
where s.deptid = d.deptid
group by d.dname
having count(s.staffid) > 
  ( select avg(deptcount)
      from ( select d.deptid, count(*) as deptcount
                      from tstaff s, tdept d
                    where s.deptid = d.deptid
                  group by d.deptid ) as deptcounts
  )
caution: untested

cheers! I will give it a go - thanks a million.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On