Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

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, 09: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, 09:38
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,561
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, 09: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, 10: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, 11:04
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,561
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 11:16.
Reply With Quote
  #6 (permalink)  
Old 04-30-03, 11: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

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