如何从联接表中获取最大值,联接表将包含多个条件?

时间:2019-05-13 20:07:55

标签: sql sql-server tsql

我已经创建了投票表。

  1. 国家名称
  2. 问题(带有ID)
  3. 答案(带有ID)
  4. 回复(用户回答问题的地方)

我需要按国家/地区获得最受欢迎的答案。 换句话说,我必须从COUNT个“国家”中选择MAX值

SELECT Country, AnswersID, COUNT(AnswersID) AS TTL
FROM Responses 
LEFT JOIN Players AS Final ON Final.PlayersID = Responses.PlayersID
GROUP BY AnswersID, Country

此代码给出如下输出:

Country | AnswersID | Total
-----------------------------
xxx     |     2     | 5
-------------------------------
xxx     |     1     | 2
-------------------------------
yyy     |     2     | 3
------------------------------
yyy     |     1     | 2

我期望的是

xxx 2 5
--------------------
yyy 2 3

2 个答案:

答案 0 :(得分:1)

您可以使用row_number()为每个记录分配一个编号,该记录按每个国家/地区的降序排列。

SELECT country,
       answerid,
       ttl
       FROM (SELECT country,
                    answersid,
                    count(answersid) ttl,
                    row_number() OVER (PARTITION BY country
                                       ORDER BY count(answersid) DESC) r
                    FROM responses 
                         LEFT JOIN players final
                                   ON final.playersid = responses.playersid
                    GROUP BY answersid,
                             country)
       WHERE r = 1;

使用row_number()在每个国家/地区将仅获得一条记录,即使两个或多个答案共享最大计数也是如此。如果要显示所有记录的最大值,则可以使用rank()

答案 1 :(得分:1)

大概是将国家/地区分配给玩家,而不是响应。如果是这样,则不需要外部联接(当然也不需要以responses开头的联接):

SELECT p.Country, r.AnswersID, COUNT(*) AS TTL
FROM Responses JOIN
     Players p
     ON p.PlayersID = r.PlayersID
GROUP BY p.Country, r.AnswersID

然后,您可以使用窗口功能获取每个国家/地区的最高总额。有关系时,使用rank()获得所有最佳答案;使用row_number()仅获得一行:

SELECT Country, AnswersID, TTL
FROM (SELECT p.Country, r.AnswersID, COUNT(*) AS TTL,
             RANK() OVER (PARTITION BY p.Country ORDER BY COUNT(*) DESC) as seqnum
      FROM Responses r JOIN
           Players p
           ON p.PlayersID = r.PlayersID
      GROUP BY p.Country, r.AnswersID
     ) c
WHERE seqnum = 1;

在统计信息中,此最常见的值称为 mode