如何获得最大计数

时间:2016-12-01 15:11:01

标签: sql-server

我在大学项目的堆栈交换数据库上工作。我想每年获得最受欢迎的标签。

我已提出此请求:

SELECT tagName
     , year(creationDate) AS annee
     , count(tagName) AS nbApparitions
FROM tags
JOIN postTags 
ON (tags.Id = postTags.tagId)
JOIN posts
ON (postTags.postId = posts.Id)
GROUP BY tagName, year(creationDate)

此请求的结果是每个标签和每年的计数。我想这样:

2016 JavaScript 123456
2015 PHP 123456
2014 HTML 123456
...

提前致谢!

1 个答案:

答案 0 :(得分:2)

SELECT tagName , annee , MAX(nbApparitions)
FROM  
  (
    SELECT tagName
         , year(creationDate) AS annee
         , count(tagName) AS nbApparitions
   FROM tags
   JOIN postTags ON (tags.Id = postTags.tagId)
   JOIN posts    ON (postTags.postId = posts.Id)
   GROUP BY tagName, year(creationDate)
)A
GROUP BY tagName , annee