Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to sort the data based on average no. of occurances..
studentid, professorid, lectureid (these are my column names....)
I need to sort the data based on average no.of students per professor...
For ex: A professor might have 65 students, with 4 different lectureids, the average is 16, like this which professors average is highest, that professor students must be listed first)

I am able to get the professorid and average count of students in another table, but I am not able to sort it in the same table!!..And using-- count(*) over(partition by professorid) as [studentcount] this statement I am able to sort the data based on highiest count..but not able to do averaging..

studentid   professorid  lectureid
1            101          401
2            101          402  
3            101          401
4            101          402
5            102          403
6            102          404
7            103          405
8            104          406
Posted
Updated 26-Dec-13 2:22am
v2
Comments
King Fisher 26-Dec-13 8:10am    
can u show the table structure here with example datas

Based on your sample data, i think you need to use ORDER BY with aggregate function, like: COUNT().

SQL
DECLARE @tmp TABLE(studentid INT IDENTITY(1,1), professorid INT, lectureid INT)
 
INSERT INTO @tmp (professorid, lectureid)
VALUES(101, 401), (101, 402),
(101, 401), (101, 402),
(102, 403), (102, 404),
(103, 405), (104, 406)
 

SELECT professorid, CountOfStudents / CountOfLectures AS AvgCount
FROM (
	SELECT professorid, COUNT(studentid) AS CountOfStudents, COUNT(DISTINCT lectureid) AS CountOfLectures
	FROM @tmp
	GROUP BY professorid
) AS T
ORDER BY CountOfStudents / CountOfLectures  DESC
 
Share this answer
 
v2
Comments
2012programmer 26-Dec-13 15:07pm    
I need the all the studentids sorted by maximumu avergestudents....
1. Professor=101 has 4 lectureids=401,411,412,415, and each lectureid has various no. of students(25,26,20,19)..
The averagecount=Totalno.ofstudents(25+26+20+19)/no.of distinct lectureids(4)=23
2. Professor=102 has 3 lectureids=416,417,419 and each lectureid has various no. of students(20,19,21)..
The averagecount=Totalno.ofstudents(20+19+21)/no.of distinct lectureids(3)=20
....
Like this there will be 60 professors, with different averagecount...My table should be sorted in a way that, If professor=119 has averagecount=24(for ex: highest)..then all the students of this professor should appear first..
Maciej Los 26-Dec-13 15:47pm    
Have a look at updated answer ;)
Use a CTE to calculate your averages, then join to that CTE to create an order by

SQL
DECLARE @tmp TABLE(studentid INT IDENTITY(1,1), professorid INT, lectureid INT)

INSERT INTO @tmp (professorid, lectureid)
VALUES(101, 401), (101, 402),(101, 402),(101, 402),(101, 402),(101, 402),
(101, 401), (101, 402),
(102, 403), (102, 404),(102, 404),(102, 404),(102, 404),
(103, 405), (104, 406);

select * from @tmp;

with avgStudents as
(
 select professorId, count(studentid)/count( distinct lectureid) as average from @tmp group by professorId
)

select t.studentid, av.professorid, t.lectureid, av.average from @tmp t
inner join avgStudents av on av.professorId = t.professorId
order by av.average desc
 
Share this answer
 
Comments
Maciej Los 26-Dec-13 15:48pm    
Nice one ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900