简体   繁体   中英

How count max of a group?

I need a command that would pick out the most count of positive issues from an author, for example, the result should look like this:

Author     WithTheMostMax
Author1                2

AUTHOR TABLE

AuthorID    AuthorName
Author1    AuthorName1
Author2    AuthorName2
Author3    AuthorName3

ISSUES LIST

AuthorID    Issues
Author1          1
Author2          0
Author3          0
Author1          1
SELECT AuthorName, SUM(Issues) AS WithTheMostMax FROM issues_list INNER JOIN issues_list.AuthorID = author_table.AuthorID
GROUP BY AuthorName ORDER BY SUM(Issues) DESC

You can use count() if 1 issue per author is stored in each row in issues table.

SELECT `authors`.`authorID`, COUNT(`issues`) as `maxIssues`
FROM `issues_list` INNER JOIN `authors` ON   
`issues_list`.`authorID`=`authors`.`authorID`
WHERE `issues`>0
GROUP BY `issues_list`.`authorID`
ORDER BY `maxIssues` DESC

http://sqlfiddle.com/#!9/bbb79/2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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