Sql 查询分组和限制

Sql Query Grouping and Limiting

我有以下 sql 查询是根据 topicName 列分组的(它还进行了一些除法运算)。 我想为每个分组主题而不是全部获得 2 行。

    SELECT wwt.topicName, t.topic_cnt as sumOfWordsInTopic,
               wwt.word, wwt.wordCount,
               (wwt.wordCount / t.topic_cnt) AS wordProbability
         FROM weightallofwordsintopic  as wwt JOIN
             (SELECT  topicName, sum(wordCount) AS topic_cnt
              FROM weightallofwordsintopic 
              GROUP BY topicName
            ) t 
         ON wwt.topicName = t.topicName

weightallofwordsintopic table 为 ;

topicName | word | wordCount
---
topic0  | word1     | 10  
topic0  | word2     | 20  
topic0  | word3     | 30  
topic0  | word4     | 40  
topic0  | word5     | 50  
topic0  | word6     | 60 

topic1  | word7     | 10  
topic1  | word8     | 20  
topic1  | word9     | 30  
topic1  | word10    | 40  
topic1  | word11    | 50  
topic1  | word12    | 60 

topic2  | word13    | 10  
topic2  | word14    | 20  
topic2  | word15    | 30  
topic2  | word16    | 40  
topic2  | word17    | 50 
topic2  | word18    | 60 

我希望输出是(根据重量排序,但这里我只是放了一个示例(select 上面的查询 returns 一些不同的列)) 我想根据每个分组的主题名称在列中的权重将上述查询限制为 2 行。

topicName | word | wordCount

topic0  | 1     | 60  
topic0  | 1     | 50  

topic1  | 1     | 60 
topic1  | 1     | 50  

topic2  | 1     | 60 
topic2  | 2     | 50  

在MySQL中,可能最简单的方法就是使用变量:

SELECT t.*
FROM (SELECT wwt.topicName, t.topic_cnt as sumOfWordsInTopic, wwt.word, wwt.wordCount,
              (wwt.wordCount / t.topic_cnt) AS wordProbability,
              (@rn := if(@t = wwt.topicName, @rn + 1,
                         if(@t := wwt.topicName, 1, 1)
                        )
              ) as rn
      FROM weightallofwordsintopic  as wwt JOIN
           (SELECT topicName, sum(wordCount) AS topic_cnt
            FROM weightallofwordsintopic 
            GROUP BY topicName
           ) t 
           ON wwt.topicName = t.topicName CROSS JOIN
           (SELECT @t := '', @rn := 0) params
      ORDER BY wwt.topicName, wwt.wordCount DESC
     ) t
WHERE rn <= 2;

我自己很新,但我相信如果您在 SELECT 语句中使用 TOP,就可以解决问题。就像是: 'SELECT TOP 2 wwt.topicName, 等等..