Sql查询分组和限制

时间:2017-04-30 13:48:01

标签: mysql sql group-by query-optimization limit

我有以下sql查询,它根据topicName列进行gruping(也进行了一些除法操作)。  我想为每个分组主题获得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 is as;

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 

我想要输出(根据它们的重量排序,但在这里我只是放一个样本(选择上面的查询返回一些不同的列)) 我想根据列中的权重将上面的查询限制为每个分组的topicName的2行。

topicName | word | wordCount

topic0  | 1     | 60  
topic0  | 1     | 50  

topic1  | 1     | 60 
topic1  | 1     | 50  

topic2  | 1     | 60 
topic2  | 2     | 50  

2 个答案:

答案 0 :(得分:0)

在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;

答案 1 :(得分:0)

我自己很新,但我相信如果你使用TOP,那么你的SELECT语句就可以了。就像是: &#39; SELECT TOP 2 wwt.topicName等。