Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help!!!!

Author  Topic 

wbmstrz
Starting Member

2 Posts

Posted - 2006-03-28 : 18:19:57
Hi everybody and thanks for reading my post.
I need help with this table: Staff(Name, SSN, Salary, SupervisorSSN)
I need to list for each supervisor:
ssn, name, the number of his/her direct (immediate) subordinates, the average salary of the subordinates, also the average no. of direct dubordinates per supervisor (one value for the entire table).
I'm new at this, I have two queries so far. I don't know how to combine the queries to give me the above. And I can't figure out how to get the list for the average no. of direct subordinates per superv. Much thanks to those who reply.

select ssn, name as superName
from staff
where ssn in
(select distinct superssn
from staff);

select superssn, avg(salary)as subsAvgSalary, count(*) as totalSubs
from staff
group by superssn;

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2006-03-28 : 18:36:21
Yay more homework.

What options have you tried ?


Damian
"A foolish consistency is the hobgoblin of little minds." - Emerson
Go to Top of Page

wbmstrz
Starting Member

2 Posts

Posted - 2006-03-28 : 19:05:15
quote:
Originally posted by Merkin

Yay more homework.

What options have you tried ?


Damian
"A foolish consistency is the hobgoblin of little minds." - Emerson



At the bottom of my post I wrote the queries that I have so far.
Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2006-03-29 : 03:42:51
You could try this -

SET NOCOUNT ON

BEGIN TRAN

CREATE TABLE Staff(Name varchar (25), SSN int, Salary money, SupervisorSSN int)
INSERT INTO Staff VALUES ('Biggus Dickus',1,200,2)
INSERT INTO Staff VALUES ('Incontinentia Buttucks',2,200,5)
INSERT INTO Staff VALUES ('Seetonya Face',3,200,2)
INSERT INTo Staff VALUES ('Hugh Jass',4,200,5)
INSERT INTo Staff VALUES ('Amanda Hugenkiss',5,1000,0)



SELECT avg(salary)as st1, count(*) / 1.0 as st2, SupervisorSSN as st3
INTO avsalnumspersup
FROM staff
GROUP BY SupervisorSSN
ORDER BY SupervisorSSN

select * from avsalnumspersup

SELECT AVG(st2) as [Avnuofsupersuper] FROM avsalnumspersup

SELECT Staff.SSN , Staff.[Name], CAST (st2 AS INT) AS [numb], st1
FROM avsalnumspersup INNER JOIN Staff
ON Staff.SSN = St3
ORDER BY Name

DROP TABLE avsalnumspersup
DROP TABLE Staff

ROLLBACK
SET NOCOUNT OFF



-------
Moo. :)
Go to Top of Page
   

- Advertisement -