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 > Database Server Software > MySQL > Help with SQL query (Calculate a ratio between two entitiess)Hi, I’m going to calcul

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-11-10, 08:13
Mestika Mestika is offline
Registered User
 
Join Date: Apr 2010
Posts: 6
Help with SQL query (Calculate a ratio between two entitiess)Hi, I’m going to calcul

Hi,

I’m going to calculate a ratio between two entities but are having some trouble with the query.

The principal is the same to, say a forum, where you say:
A user gets points for every new thread. Then, calculate the ratio of points for the number of threads.

Example:
User A has 300 points. User A has started 6 thread. The point ratio is: 50:6

My schemas look as following:

student(studentid, name, class, major)
course(courseid, coursename, department)
courseoffering(courseid, semester, year, instructor)
faculty(name, office, salary)
gradereport(studentid, courseid, semester, year, grade)

The relations is a following:

Faculity(name) = courseoffering(instructor)
Student(studentid) = gradereport (studentid)
Courseoffering(courseid) = course(courseid)
Gradereport(courseid) = courseoffering(courseid)

I have this query to select the faculty names there is teaching one or more students:

Code:
    SELECT COUNT(faculty.name) FROM faculty, courseoffering, gradereport, student WHERE faculty.name = courseoffering.instructor AND courseoffering.courseid = gradereport.courseid AND gradereport.studentid = student.studentid
My problem is to find the ratio between the faculty members salary in regarding to the number of students they are teaching.

Say, a teacher get 10.000 in salary and teaches 5 students, then his ratio should be 1:5.

Hope that someone has an answer to my problem.

Thanks

Mestika
Reply With Quote
  #2 (permalink)  
Old 04-11-10, 08:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
we'll walk you through it

first, you need to get the number of students taught by each faculty member

hint: add a GROUP BY to your query

also, a suggestion: use JOIN syntax, not the old style "comma" joins
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-12-10, 05:03
Mestika Mestika is offline
Registered User
 
Join Date: Apr 2010
Posts: 6
I got it to work, but thanks for your help. Used the following query:

SELECT DISTINCT faculty.name, faculty.salary / (
SELECT COUNT( student.studentid ) AS StudentPerTeacher
FROM student, faculty, courseoffering, gradereport
WHERE student.studentid = gradereport.studentid
AND gradereport.courseid = courseoffering.courseid
AND courseoffering.instructor = faculty.name ) AS "Ratio Per Student"
FROM faculty
ORDER BY "Ratio Per Student" DESC
Reply With Quote
  #4 (permalink)  
Old 04-12-10, 05:06
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
you "got it to work" but it isn't producing the correct answer
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Tags
mysql, mysql query, query, query performance, sql

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