MySQL - 选择最大总和

标签 mysql

我有一个 MySQL 表,如下所示。

**AuthorID**, **PublicationName**, ReferenceCount, CitationCount

AuthorID 和 PublicationName 作为主键。我需要找到所有作者的 ReferenceCount 和 CitationCount 的最大总和。例如,数据如下。

1 AAA 2 5
1 BBB 1 3
1 CCC 2 4
2 AAA 1 4

在这种情况下,我需要我的输出,

1 AAA 7
2 AAA 5

我尝试了以下查询。

SELECT AuthorID, PublicationName, Max(Sum(ReferenceCount + CitationCount)) 
from Author 
Group by AuthorID, PublicationName 

如果我使用 max(sum(ReferenceCount + CitationCount)) 按 AuthorID、PublicationName 分组,我会收到“组函数的无效使用”错误。我相信我应该在查询中包含 Having 子句。但我不确定如何做同样的事情。

最佳答案

如果我对这个问题的理解是正确的,那么您需要引用次数最多的出版物的所有记录。出版物及其引用计数由下式给出:

SELECT PublicationName, Sum(ReferenceCount + CitationCount)
from Author
Group by PublicationName
order by Sum(ReferenceCount + CitationCount) desc
limit 1;

order bylimit 1 为您提供最高值。

如果您想要具有最大总和的出版物的所有记录:

select a.*
from Author a join
     (SELECT PublicationName, Sum(ReferenceCount + CitationCount)
      from Author
      Group by PublicationName 
      order by Sum(ReferenceCount + CitationCount) desc
      limit 1
     ) asum
     on a.PublicationName = asum.PublicationName

关于MySQL - 选择最大总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18617587/

相关文章:

mysql - MySQL 中连接两个行数不同的表

php - 当 php/mysql if 语句为真时播放声音片段?

MYSQL SP 不循环

mysql - 搜索引擎爬虫和 SQL

mysql - 插入错误 #1452 - 无法添加或更新子行 : a foreign key constraint fails

mysql - 像 MySQL 触发器但作为一个整体?

php - 为什么我无法向 MySQL 插入数据?

Mysql 5.5 表分区用户和好友

php - 从两个不同的表中获取 MySQL 值到一个数组中?

java - 如何在preparedstatement中使用JDBC通配符创建mysql表