Calculate average in SQL query - php

I am trying to calculate the class average of a class where students can hold different amounts of subjects (e.g.: two students in a class of which one takes 3 subjects and the other 2). How do I get the class average that applies to all students? I already have their total term score in a table. I know the formula for calculating the average is simply the total averages of individual students divided by the number of students, but I can't seem to get it working. Can anyone please help me write a query that will calculate and print out this class average?
Here is what I have tried so far:
SELECT student_id, SUM(CA_total)/count(term_total) AS average
FROM score_entry
GROUP BY student_id
HERE IS THE OUTCOME. result from database
BUT I want the final result to be the sum of 25 + 26.5 which will be 51.5 then divided by the 2 students in the class should give 25.75. this is the class average. I want the query or code that will echo out the final answer 25.75. thanks

You are looking for SQL AVG function. More about it here: http://www.w3schools.com/sql/sql_func_avg.asp

Related

get highest point player within salary range

I need a way to get highest points players within salary range i.e 50,000
There is a similar question here Algorithm to select Player with max points but with a given cost.
Basically I have to select optimal 9-player line-up.
I googling lot and I found this can be achieve using linear programming.But I don't know how can I use Lp in php.
Any idea how can I achieve this or there is any other way to do this?
If you store the information in arrays, I believe you could achieve the result using array_multisort which would give result similar to SQL order by. For example, order by points DESC, salary ASC. This would give back the array having top points players at top and if any of them have the same amount of points, the first would be the one with the lowest salary.
The answer to this question shows how to use array_multisort.

Get lowest sum and highest sum of users from MySQL

I have a MySQL database that is holding users activities on the site. Each time the user completes something I log the amount of time it took them to complete. The field is stored as a decimal.
I am wanting to know how I can get the lowest sum and the highest sum amounts of users. Lets say user1 has performed 2 tasks taking .5 each. Their sum would be 1.0. User2 has performed 10 tasks each taking 1.5 each. Their sum would be 15. User3 has performed 20 tasks each taking .25 each. Their sum would be 5.
So running a query over the DB the lowest amount would be 1 and the highest amount would be 15.
I know how to get the sum of columns but not sure how to return the lowest and the highest.
Thanks.
You can do this with a subquery and two levels of aggregation:
select min(totaltime), max(totaltime)
from (select user, sum(amountoftime) as totaltime
from t
group by user
) t;
Actually getting the users associated with the min and max is a bit more difficult, but that is not this question.

Accurate star rating calculation

I need to calculate a star rating for a product
I know how to calculate the weighted average, but its not good enough
example (5*252 + 4*124 + 3*40 + 2*29 + 1*33) / (252+124+40+29+33) = 4.11
I want to avoid cases when a product get a 1000 five star ratings and one 4 star, and another one gets just one 5 stars and it gets on top
I know there is a way but i couldn't find it
thanks
Try sort you product record using new column (rating * votes).
it will help you to find the most voted product with best rating.
use sorting which can include number of ratings, something like number of votes divided by calculated avg rating.
You can multiply for a weight function, that gives a penalty to product with lower number of votes and converge in time. Something like this should do the job.
a parabole truncated to 1 should do the job
EX:
convergence_step=1000
if voters<convergence_step:
meanscore=score*{[(voters)/(float)(convergence_step)]^2}
else
meanscore=score

MySQL view to generate monthly bonus

I'm building a web site for marketing company. As per their requirement, when a customer makes a booking. A certain amount of bonus is distributed between employees based on
their hierarchy. The distribution starts from 60 days after booking and bonus is given
for 24 months.
The tables are
bookings
bid book_date
1 2012-05-09
2 2012-05-10
bonus
bid empid amount
1 1 300
1 2 400
2 2 300
2 3 400
Is it possible to write mysql views that generates monthly bonus an employee gets
for every month. I didn't find solution on how to make update with mysql view. Any hint
will of great help.
Instead of view, I would suggest is write mysql function which will return the bonus by accepting the employee ID.
Using mysql function you will have more room to write logic and PL/SQL.
Inner join on bid and filter to only include eligible bonuses by comparing the book date to today's date. If today's date is less than 60 days after or more than 24 months plus 60 days after the original book date, exclude it. (You can go to mySQL.com to learn more about how to manipulate dates in mySQL. I forget...)
You will be left with multiple rows containing only emp id and amount. In the second round, use a "select sum(amount) from (...put your other query here...) group by empid" to get the aggregate bonus per employee.
This approach (and I think any solution) requires a nested SQL statement, and so if you're not comfortable with that syntax you can use that term to explore in google or SO. Cheers!

Build a Ranking

I have a newssystem where you can rate News with 1 to 5 stars. In the Database i save the count, the sum and the absolute rating as int up to 100 (for html output, so 5 stars would be 100 1 star would be 20percent.
Now i have three toplists:
Best Rated
Most viewed
Most commented
Last two ones are simple, but the first is kinda tricky.
Before i took that thing over it was all a big mess, and they just put the 5 best rated news there, so in fact if there was a news rated 4.995 with 100k votes and another one with 5 stars at 1 vote, the "better rated" one is on top even if that is obv ridiculous.
For the first moment i capped the list so only news with a certain amount of votes (like 10 or 20) can be in the list.
But i do not really like that. Is there a nice method to kind-a give those things a "weight" with the count or something like that?
Have you considered using a weighted bayesian rating system? It'll weight the results based on the number of votes and the vote values themselves.
You could explore the statistical confidence in the rating perhaps based around the average rating received for all entries and the standard deviation of all votes. While an entry has an average rating of 5, if you only have a few votes then you may not be able to say with more than 90% confidence that the actual rating is above 4.7 say. You can then rate the entries based upon the rating for which you have 90% confidence.
I'm not sure if this meets your requirement of being simple.
You could use median of the user ratings as the total rating.
You would have five fields with eatch article, each one containing how many times the article was rated as n stars. Then you would select the field with the biggest value of all these and that would be your rating. It has the advantage of ignoring the outliers in the ratings.

Categories