-2

これは私を夢中にさせています!MySQL DBMSをミューズします(それを指定して申し訳ありません)...

私は次の表を持っています:

"id", "pingdate", "players"
"3","2012-05-24 11:45:04","63"
"1","2012-05-24 12:15:03","14"
"2","2012-05-24 12:15:03","38"
"3","2012-05-24 12:15:03","24"
"1","2012-05-24 12:45:04","22"
"2","2012-05-24 12:45:04","40"
"3","2012-05-24 12:45:04","66"
"1","2012-05-24 13:15:03","14"
"5","2012-05-24 13:15:03","99"
"3","2012-05-24 13:15:03","63"
"3","2012-05-24 13:15:03","38"
"4","2012-05-24 13:15:03","63"

次の結果が欲しい:

"5","2012-05-24 13:15:03","99"
"3","2012-05-24 12:45:04","66"
"4","2012-05-24 13:15:03","63"

テキストでは、最高のプレーヤーと正しいpingdateを持つ3つの異なるIDが必要です。

これを試しましたが、間違ったpingdateが返されます。

SELECT id, 
       pingdate, 
       max(players) AS players 
FROM pings 
GROUP BY id  
ORDER BY players DESC 
LIMIT 3
4

4 に答える 4

1

DBMSを指定しなかったため、これはANSISQLソリューションです。

select *
from (
    select id, 
           pingdate,
           rank() over (partition by id order by players desc) as rnk
    from pings
) t
where rnk <= 3
order by players desc
于 2012-06-10T15:21:31.913 に答える
0

結合またはサブクエリを使用して実行する必要があります。SQLを参照してください-groupbyに基づいて行からIDを取得します

Select  a.id, a.pingdate, a.players from pings a, (
  SELECT id, max(players) AS players 
  FROM pings 
  GROUP BY id ) b 
where a.id=b.id and a.players=b.players 
ORDER BY players DESC 
LIMIT 3
于 2012-06-10T15:15:52.667 に答える
0

最初にすべての結果を返す必要があります....次に3つだけを返します

SELECT *
FROM (
SELECT id,pingdate, max(players) AS players 
FROM pings 
GROUP BY id, pingdate  )
WHERE rownum = 3
ORDER BY players DESC 

これがお役に立てば幸いです

于 2012-06-10T15:16:36.947 に答える
0

maximalの定義:=これ以上の値はありません:

SELECT id, pingdate, players 
FROM pings pi
    WHERE NOT EXISTS (
         SELECT *
         FROM pings nx
         WHERE nx.id = pi.id
         AND nx.players > pi.players
         )
ORDER BY players
LIMIT 3
    ;
于 2012-06-10T15:24:16.810 に答える