简体   繁体   English

MySQL按平均两个平均值排序

[英]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. 完成后,相关的条目ID将附加一个排名值,然后可用于确定比赛中哪个条目获得最高的平均分数。 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. 我希望做的是最终得到一个表格,显示特定比赛中的每个条目,标题,然后显示3个值,该条目的avg_normal,该条目的avg_judge,然后将这两个值相加并除以2 ,所以avg_normal和avg_judge每个帐户占avg_all的50%。 Finally, sort the table by avg_all. 最后,通过avg_all对表进行排序。

avg_all = ((avg_normal + avg_judge) / 2) avg_all =((avg_normal + avg_judge)/ 2)

They order entry_ids 1, 2, 3, 4, 5 in order. 他们按顺序订购entry_ids 1,2,3,4,5。 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 我希望能够确定1-100的平均值,所以排名为0 = 100分,1 = 90,2 = 80,3 = 70,以及4以上= 5分

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 1.)普通用户投票得分

2.) The average JUDGE user vote score 2.)JUDGE用户平均投票得分

3.) The average of the NORMAL & JUDGE SCORE. 3.)NORMAL和JUDGE SCORE的平均值。

So Normal User average = 93.3333, Judge average = 70, Total Average = 81.66665 所以普通用户平均值= 93.3333,法官平均值= 70,总平均值= 81.66665

Thanks to the answers below, both queries work like a champ. 感谢下面的答案,两个查询都像冠军一样。

Please note the following: 请注意以下事项:

  • I've assumed that there is a field user_type in members which stores either 'NORMAL' or 'JUDGE' 我假设成员中有一个字段user_type存储'NORMAL'或'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. 我已经删除了titles.title的数据和组的连接,因为我看不出它们与你的平均值有什么关系。

.

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;

All this change does it wrap the original query, lines314159 has the bulk of the work 所有这些更改都包装了原始查询,lines314159具有大部分工作

SELECT aa.title,aa.avg_normal,aa.avg_judge,(aa.avg_normal + aa.avg_judge) / 2 AS avg_all
from 
(
SELECT
t.title, 
AVG(CASE WHEN group_id = '6' 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 group_id = '7' 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
FROM exp_rankings r
LEFT JOIN exp_weblog_titles t
  ON r.entry_id = t.entry_id
LEFT JOIN exp_members m
  ON t.author_id = m.member_id
WHERE r.contest_id IN ('22')
GROUP BY
  t.title
ORDER BY
 avg_all) as aa;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM