1
SELECT plaka, party_id, sum(inf) i 
  FROM party_sehir
 GROUP BY party_id, plaka

This is my sub-query and the results are:

plaka   party_id   i 
34      1          42
34      2          9
35      1          11
35      2          26

from these results I want to get maximum i of every plaka and rows party_id, like this:

plaka  party_id    i 
34     1           42
35     2           26

I mean the maximum for each plaka. I also tried:

SELECT * 
  FROM ( SELECT plaka, party_id, sum(inf) i 
           FROM party_sehir
          GROUP BY party_id, plaka ) t1 
 GROUP BY plaka  
HAVING i >= max(i)

It didn't work; is anyone able to help?

thank you

4

2 回答 2

2

它需要更长的查询,例如

select plaka,party_id, i 
from
(SELECT plaka,party_id, sum(inf) i FROM party_sehir
 GROUP BY  party_id, plaka) sum_group
where (i,plaka) in (
SELECT max(i), plaka
 FROM (SELECT plaka,party_id, sum(inf) i FROM party_sehir
 GROUP BY  party_id, plaka) t1 group by plaka ) 
于 2012-05-05T09:38:22.477 回答
0

请尝试 group by 以获得您的解决方案

SELECT plaka ,party_id   , max( `i` ) FROM `party_sehir` GROUP BY `plaka`

或者

SELECT * FROM `party_sehir` AS s1 WHERE s1.i = (SELECT max( `i` ) FROM party_sehir AS  
   s2  WHERE s1.plaka = s2.plaka ) GROUP BY `plaka`
于 2012-05-05T10:39:11.813 回答