MySQL sort by average of two averages

I am working on a contest site where there are two types of users, normal site members, and judges. Each can use a drag and drop tool to order the entries in a particular contest in the order they choose. When they are done the relevant entry ids are attached a ranking value that can then be used to determine which entry in contest got the highest average score. The winner will actually be determined by the averaging the averages of each group.

What I hope to do is end up with a table showing EACH entry in a particular contest, with the title, and then show 3 values, avg_normal for that entry, avg_judge for that entry, and then those two values added together and divided by two, so the avg_normal and avg_judge each account for 50% of the avg_all. Finally, sort the table by avg_all.

avg_all = ((avg_normal + avg_judge) / 2)

They order entry_ids 1, 2, 3, 4, 5 in order. The ranking value starts at zero so:

entry_id, entry_ranking, author_id
1, 0, 1
2, 1, 1
3, 2, 1
4, 3, 1
5, 4, 1

I'm hoping to determine the averages on a scale of 1-100, so an entry ranking of 0 = 100 points, 1 = 90, 2 = 80, 3 = 70, and anything above 4 = 5 points

Each user is attached to a group in another table, so they are either a normal user, or a judge

I want to be able to write a query that finds

1.) The average NORMAL user vote score

2.) The average JUDGE user vote score

3.) The average of the NORMAL & JUDGE SCORE.

So Normal User average = 93.3333, Judge average = 70, Total Average = 81.66665

Thanks to the answers below, both queries work like a champ.

This question and answers originated from www.stackoverflow.com
Question by (2/11/2010 10:25:33 PM)

Answer

Please note the following:

  • I've assumed that there is a field user_type in members which stores either 'NORMAL' or 'JUDGE'

  • I've removed the join to data and the group by of titles.title because I don't see how they're relevant to your averages.

.

SELECT
  t.title,
  AVG(CASE WHEN user_type = 'NORMAL' THEN IF (r.ranking_value = '0', 100, 0) + IF (r.ranking_value = '1', 90, 0) + IF (r.ranking_value = '2', 80, 0) + IF (r.ranking_value = '3', 70, 0) + IF (r.ranking_value = '4', 5, 0) END) AS avg_normal,
  AVG(CASE WHEN user_type = 'JUDGE' THEN IF (r.ranking_value = '0', 100, 0) + IF (r.ranking_value = '1', 90, 0) + IF (r.ranking_value = '2', 80, 0) + IF (r.ranking_value = '3', 70, 0) + IF (r.ranking_value = '4', 5, 0) END) AS avg_judge,
  (AVG(CASE WHEN user_type = 'NORMAL' THEN IF (r.ranking_value = '0', 100, 0) + IF (r.ranking_value = '1', 90, 0) + IF (r.ranking_value = '2', 80, 0) + IF (r.ranking_value = '3', 70, 0) + IF (r.ranking_value = '4', 5, 0) END) +
  AVG(CASE WHEN user_type = 'JUDGE' THEN IF (r.ranking_value = '0', 100, 0) + IF (r.ranking_value = '1', 90, 0) + IF (r.ranking_value = '2', 80, 0) + IF (r.ranking_value = '3', 70, 0) + IF (r.ranking_value = '4', 5, 0) END)) / 2 AS avg_all
FROM rankings r
LEFT JOIN titles t
  ON r.entry_id = t.entry_id
LEFT JOIN members m
  ON t.author_id = m.member_id
WHERE r.contest_id IN ('CONTEST ID NUMBER')
GROUP BY
  t.title
ORDER BY
  avg_all;
Answer by

Find More Answers
Related Topics  mysql  sql  query  sorting  average
Related Questions
  • What's the most efficient way to get the horizontal average in a MySQL query?

    I have the following MySQL-table Id | One | Two | Three ---------------------------- 1 | 10 | 30 | 20 2 | 50 | 60 | 20 3 | 60 | NULL | 40 Edit: Of course the table doesn't need to be …
  • SQL Compute Average of Two Fields

    I have a database table which has three relevant fields, firstly a user id, then one is a number of submitted nodes, the other is a sum of the "score" for each node. I need to be able to run a qu…
  • Ordering by the difference between two averages

    I have a single table called orders. It has 3 fields I care about: price, type, and bid. Bid is an int with either 0 or 1 depending on if the order is to buy or sell something. 1 is buy, 0 is sell. …
  • Mysql Average on time column?

    SELECT avg( duration ) as average FROM `login`; The datatype for duration is "time", thus my value is like: 00:00:14, 00:20:23 etc I execute the query it gives me: 2725.78947368421 What is…
  • MDX query for average of sums by month and year

    I'm stuck on a MDX query... I have a table which looks like: Date Client Amount --------------------------------- 2010-01-01 1 1000 2010-01-01 2 500 2010-01-02 1 1100…
  • calculate averages for 5 min intervals in mysql

    I have a table log with columns id, value, category and timestamp. Suppose the table is filled like this: ID VALUE CATEGORY TIMESTAMP 1 10 1 2010-11-1 10:00:00 2 20 …
  • Table of averages by date for multiple IDs

    I have a table full of items that each have a unique ItemID . There is another table with information on tests that are done on these items around once a month (some may have multiple tests per mont…
  • Select average from MySQL table with LIMIT

    I am trying to get the average of the lowest 5 priced items, grouped by the username attached to them. However, the below query gives the average price for each user (which of course is the price), …
  • Getting the average number of orders per day using mysql

    I have the following table structure: ID, User_ID, DateTime Which stores a user id and datetime of an order purchased. How would I get the average number of orders a day, across every row? …
  • MySQL or PHP date averages

    I am trying to figure out how to find the average of a number of different time values, the time values are time differences from 2 dates so they will be in the format of "hh:mm:ss". So if I had 4 t…