【问题标题】:I need one string with max value我需要一个具有最大值的字符串
【发布时间】:2020-11-26 12:58:26
【问题描述】:

我有这样的疑问:

SELECT s_name, count(Mark)
FROM specialty
join interns_specialty on specialty.specialty_id = interns_specialty.specialty_id
join practice_result on practice_result.Intern_id = interns_specialty.intern_id
where Mark=5
group by specialty.specialty_id
ORDER by count(Mark) DESC;

That is what i get from my quire, i need to take the row with the max count of excellent marks, if there is two or more such rows, i must to get it all 我需要得到一个带有专业名称的字符串,它的分数比其他字符串高。 但是我可以t understand how to do this. I tried to use "Limit", but if i have two or more specialties - it will be work incorrect. I couldnt 将 MAX 与另一个聚合函数一起使用,所以我需要一个建议。

【问题讨论】:

  • 您通常GROUP BY 与您SELECT 相同的列,除了那些是设置函数的参数的列。
  • 如果不同的s_name(取自哪个表?)值可能匹配相同的specialty.specialty_id,那么您的查询没有意义。
  • @Akina 我不明白,你说这个查询没有意义,但它有效。它给了我特殊的标记。所以我只需要选择5个以上的专业或专业。我做错了什么?
  • @jarlh 这是什么意思?我需要做什么?
  • 条件“多个s_name匹配同一个specialty.specialty_id”是否为真?如果是这样 - 你从所有可能的值中只获得一个值......那有什么意义?

标签: mysql sql count max limit


【解决方案1】:

我想你想要rank():

SELECT m.*
FROM (SELECT s.s_name, COUNT(*),
             RANK() OVER (ORDER BY COUNT(*) DESC) as seqnum
      FROM specialty s JOIN
           interns_specialty ins
           ON s.specialty_id = ins.specialty_id JOIN
           practice_result pr
           ON pr.Intern_id = ins.intern_id
      WHERE Mark = 5
      GROUP BY s.specialty_id
     ) m
WHERE seqnum = 1;

【讨论】:

  • 但是如果是两个具有相同数量优秀分数的字符串呢?
  • @AntonTuhusov 都会返回(或许测试起来更简单?)。
  • @Akina ,спасибо, но я не могу протестить ))
  • @AntonTuhusov 为什么?测试任何在线小提琴... PS。不要在那里使用本国语言。
猜你喜欢
  • 2020-06-02
  • 2020-08-10
  • 1970-01-01
  • 2014-08-07
  • 2020-01-06
  • 1970-01-01
  • 2017-10-11
  • 1970-01-01
  • 2021-10-29
相关资源
最近更新 更多