2011-07-23 32 views
2

我有一個25場比賽的結果數據庫。然而只有每個人最好的十分數。 有人可以告訴我如何總結每個人的前十個分數,並顯示在該總數中使用的最低分數(他們的第十個最佳分數)。 該數據庫具有PlayerName,TournamentID,Points 例如。我如何獲得每個人最好的10個賽季總和?

- TounamentID PlayerName Points 
- 1    Jo   100 
- 1    Tel   50 
- 1    Kevin  75 
- 2    Jo   100 
- 2    Tel   50 
- 2    Kevin  75 
- 3    Jo   100 
- 3    Tel   50 
- 3    Kevin  75 
- 4    Jo   100 
- 4    Tel   50 
- 4    Kevin  75 
- 5    Jo   100 
- 5    Tel   50 
- 5    Kevin  75 etc 

提前感謝

編輯

目前,我有這樣的工作,雖然它不處理重複的成績非常好,可以實際最終達到增加了的如果有重複,排名前11位;

SELECT X.PlayerName, Sum(X.Points) AS SumOfPoints, Min(X.Points) AS Target 
FROM SoP11PreBats AS X 
WHERE (((10)>(SELECT count(*) 
     FROM SoP11PreBats 
     WHERE PlayerName = X.PlayerName 
     AND Points > X.Points))) 
GROUP BY X.PlayerName 
ORDER BY Sum(X.Points) DESC; 

回答

0

我有一種感覺,這是不行的:

SELECT * 
FROM 
    (SELECT pd.PlayerName 
     , (SELECT SUM(t10.Points) 
      FROM 
       (SELECT t10.Points 
       FROM SoP11PreBats AS t10 
       WHERE t10.PlayerName = pd.PlayerName 
       ORDER BY t10.Points DESC 
       LIMIT 10 
       ) AS x 
      ) AS Sum10 
     , (SELECT t10.Points 
      FROM SoP11PreBats AS t10 
      WHERE t10.PlayerName = pd.PlayerName 
      ORDER BY t10.Points DESC 
      LIMIT 1 OFFSET 9 
      ) AS TenthBest 
    FROM 
     (SELECT DISTINCT PlayerName 
      FROM SoP11PreBats 
     ) AS pd 
) AS y 
ORDER BY Sum10 DESC 

但這會:

SELECT pb.PlayerName AS PlayerName 
    , COALESCE(SUM(p.Points),0) + pb.TenthBest*(10-COUNT(p.Points)) 
         AS SumOfPoints 
    , pb.TenthBest AS Target 
FROM 
    (SELECT pd.PlayerName 
     , (SELECT t10.Points 
      FROM SoP11PreBats AS t10 
      WHERE t10.PlayerName = pd.PlayerName 
      ORDER BY t10.Points DESC 
      LIMIT 1 OFFSET 9 
      ) AS TenthBest 
    FROM 
     (SELECT DISTINCT PlayerName 
      FROM SoP11PreBats 
     ) AS pd 
) AS pb 
    LEFT JOIN SoP11PreBats AS p 
    ON p.PlayerName = pb.PlayerName 
    AND p.Points > pb.TenthBest 
GROUP BY pb.PlayerName 
ORDER BY SumOfPoints DESC 
+0

不,不這樣認爲。這與我上一篇文章的最後一次編輯相同。其中t10.Playername = pd.Playername不會編譯(找不到pd) – Eddy

+0

@Eddy:是的,我有一個預感,它不會工作。特里,嘗試更新(對不起,我現在不能測試)。 –

+0

嗨 - 這返回以下; Radek 9.只有一行,總計遠遠超過Radeks的實際得分。 –

1

像這樣的在一次對一個球員的工作:

SELECT SUM(n), MIN(n) FROM 
    (SELECT points AS n 
    FROM  table 
    WHERE PlayerName = ? 
    ORDER BY n DESC 
    LIMIT 10 
) 

我不知道如何將它擴大到生產表球員。

1
SELECT test.playername, sum(top10.score), MIN(top10.score) 
FROM test 
LEFT JOIN (SELECT playername, score FROM test ORDER BY score DESC LIMIT 10) top10 
ON top10.playername = test.playername 
GROUP BY test.playername 

編輯:原來上面的方法使用子查詢和連接是不會去做的。由於您在次選中限制了結果,因此不會導致PER玩家名稱的最大10個記錄集。
接下來的做法是像做

SELECT pk, name, score from test where 
pk IN (SELECT pk FROM test t2 WHERE t2.name = name ORDER BY score DESC LIMIT 10) 

這可能會創建正確的記錄集的加入有。但是,INIT子句中尚不支持LIMIT(尚)。所以這不會編譯。

EIDT2:最後的方法,我能想到的是這樣的:

select distinct test.name 
, (SELECT sum(t2.score) FROM (select t3.score FROM test t3 WHERE t3.name = test.name ORDER BY score desc LIMIT 10) t2) 
, (SELECT min(t2.score) FROM (select t3.score FROM test t3 WHERE t3.name = test.name ORDER BY score desc LIMIT 10) t2) 
FROM test 

這一個也不能編譯,因爲在這裏最內test.name不能得到妥善解決。

我不認爲你想要做什麼目前可以在mysql的單個查詢中完成,但我很好奇,如果有人能證明我錯了。

+0

+1那是位我無法弄清楚:) – Alnitak

+0

感謝您的快速反應的傢伙。但我有點業餘,無法讓這個工作。我有網絡服務器上的MySQL和訪問我的桌面上,通常測試訪問和剪切和粘貼到網絡服務器的SQL。有些功能需要更換,但通常可以工作。上述工作將在這個設置上?另外我的表名是SoP11PreBATS。我只是用實際的表名替換測試? –

+0

是的,你可以用'SoP11PreBats'代替'test'。它不會在訪問時運行,但會在您的網絡服務器上運行(「LIMIT 10」是特定於mysql的)。在SqlServer中,它將是「Select Top 10 player,...」,而不是「Limit 10」部分。不是100%肯定的,但這也可能在msaccess – Eddy

1

好吧,我得到它的工作,但它是關於最討厭的SQL劈我能想到的我不確定你應該考慮像這樣把它放在生產中。它也只能運行在MySQL的:

select PlayerName, sum(Points), min(Points) from 
(select distinct SoP11PreBats.PlayerName, (SELECT t2.Points FROM test t2 WHERE t2.PlayerName = SoP11PreBats.PlayerName ORDER BY Points desc LIMIT 1) as Points FROM test 
UNION ALL 
select distinct SoP11PreBats.PlayerName, (SELECT t2.Points FROM test t2 WHERE t2.PlayerName = SoP11PreBats.PlayerName ORDER BY Points desc LIMIT 1,1) as Points FROM test 
UNION ALL 
select distinct SoP11PreBats.PlayerName, (SELECT t2.Points FROM test t2 WHERE t2.PlayerName = SoP11PreBats.PlayerName ORDER BY Points desc LIMIT 2,1) as Points FROM test 
UNION ALL 
select distinct SoP11PreBats.PlayerName, (SELECT t2.Points FROM test t2 WHERE t2.PlayerName = SoP11PreBats.PlayerName ORDER BY Points desc LIMIT 3,1) as Points FROM test 
UNION ALL 
select distinct SoP11PreBats.PlayerName, (SELECT t2.Points FROM test t2 WHERE t2.PlayerName = SoP11PreBats.PlayerName ORDER BY Points desc LIMIT 4,1) as Points FROM test 
UNION ALL 
select distinct SoP11PreBats.PlayerName, (SELECT t2.Points FROM test t2 WHERE t2.PlayerName = SoP11PreBats.PlayerName ORDER BY Points desc LIMIT 5,1) as Points FROM test 
UNION ALL 
select distinct SoP11PreBats.PlayerName, (SELECT t2.Points FROM test t2 WHERE t2.PlayerName = SoP11PreBats.PlayerName ORDER BY Points desc LIMIT 6,1) as Points FROM test 
UNION ALL 
select distinct SoP11PreBats.PlayerName, (SELECT t2.Points FROM test t2 WHERE t2.PlayerName = SoP11PreBats.PlayerName ORDER BY Points desc LIMIT 7,1) as Points FROM test 
UNION ALL 
select distinct SoP11PreBats.PlayerName, (SELECT t2.Points FROM test t2 WHERE t2.PlayerName = SoP11PreBats.PlayerName ORDER BY Points desc LIMIT 8,1) as Points FROM test 
UNION ALL 
select distinct SoP11PreBats.PlayerName, (SELECT t2.Points FROM test t2 WHERE t2.PlayerName = SoP11PreBats.PlayerName ORDER BY Points desc LIMIT 9,1) as Points FROM test 
) top10 
group by name 
+0

這是輝煌的作品,但我不能保存它作爲一個視圖,這是一個更大的查詢的一小部分。我得到錯誤; #1349 - 視圖的SELECT在FROM子句中包含子查詢有沒有辦法將它分成兩個單獨的查詢? –

+0

你開始嚇唬我的男人。這已經超過了頂端,這成爲更大的查詢的一部分。你應該真的停下來一會兒,重新考慮你在做什麼,如果這是做到這一點的正確方法。如果數據集增長的話,像這樣正常執行的查詢將很難甚至不可能。 – Eddy

+0

沒關係。我吐這兩個查詢,它似乎工作。非常感謝您花費在此上的所有時間。 –

0

這工作(見下文測試輸出):

set @count:=0, @player:=''; 
SELECT 
    PlayerName, 
    SUM(Points) as sum_top_10, 
    MIN(Points) as min_top_10 
FROM (SELECT PlayerName, Points 
    FROM (SELECT 
    Points, 
     @count := if (@player != PlayerName, 0, @count + 1) as count, 
     @player := PlayerName as PlayerName 
     FROM (SELECT PlayerName, Points FROM SoP11PreBATS order by 1, 2 desc) x) y 
    where count < 10) z 
group by 1; 

這裏的測試,使用OP的數據,再加上額外行的「喬」,使超過10行:

create table SoP11PreBATS (TounamentID int, PlayerName text, Points int); 
delete from SoP11PreBATS; 
insert into SoP11PreBATS values 
(1, 'Jo', 100), (1, 'Tel', 50), (1, 'Kevin', 75), (2, 'Jo', 100), (2, 'Tel', 50), 
(2, 'Kevin', 75), (3, 'Jo', 100), (3, 'Tel', 50), (3, 'Kevin', 75), (4, 'Jo', 100), 
(4, 'Tel', 50), (4, 'Kevin', 75), (5, 'Jo', 100), (5, 'Tel', 50), (5, 'Kevin', 75), 
(5, 'Jo', 50), (6, 'Jo', 75), (7, 'Jo', 100), (8, 'Jo', 50), (9, 'Jo', 75), 
(10, 'Jo', 50), (11, 'Jo', 75), (12, 'Jo', 100); 
select * from SoP11PreBATS where playername = 'Jo' order by points desc; 
+-------------+------------+--------+ 
| TounamentID | PlayerName | Points | 
+-------------+------------+--------+ 
|   1 | Jo   | 100 | 
|   2 | Jo   | 100 | 
|   3 | Jo   | 100 | 
|   4 | Jo   | 100 | 
|   5 | Jo   | 100 | 
|   7 | Jo   | 100 | 
|   12 | Jo   | 100 | 
|   6 | Jo   |  75 | 
|   9 | Jo   |  75 | 
|   11 | Jo   |  75 | 
|   5 | Jo   |  50 | 
|   8 | Jo   |  50 | 
|   10 | Jo   |  50 | 
+-------------+------------+--------+ 
-- Inspection shows Jo should have 925 as sum and 75 as min 
-- Ran query above and got: 
+------------+------------+------------+ 
| PlayerName | sum_top_10 | min_top_10 | 
+------------+------------+------------+ 
| Jo   |  925 |   75 | 
| Kevin  |  375 |   75 | 
| Tel  |  250 |   50 | 
+------------+------------+------------+ 
-- Test output correct 
+0

嗨我試過這在我的服務器bt有以下錯誤; #1267 - 非法混合排序(utf8_unicode_ci,IMPLICIT)和(utf8_general_ci,IMPLICIT)操作'<>'你知道這意味着什麼嗎? –

+0

似乎您的編碼與此語法不兼容。 – Bohemian

+0

@Terry:可能是'@player!= PlayerName'造成這種情況。我不知道如何使用排序規則與變量雖然(所以沒有建議如何解決這個錯誤) –

0

我想這樣的作品,它僅使用一個派生表:

SELECT @row := 0, @pp := NULL, @min := 0; 
    SELECT Player, 
     SUM(Points) AS Points, 
     MIN(Points) AS MinPoints 
    FROM (
    SELECT Player, 
     Points, 
     @row := IF(
      IFNULL(@pp, '') <> Player AND NOT (@pp := Player), 
      1, 
      @row + 1 
     ) AS Row 
    FROM SoP11PreBats 
ORDER BY Player, Points DESC 
) tmp 
    WHERE tmp.Row <= 10 
GROUP BY Player; 

測試數據:

CREATE TABLE `SoP11PreBats` (
`TournamentID` int(11) NOT NULL, 
`Player` varchar(255) NOT NULL, 
`Points` int(11) NOT NULL 
); 

INSERT INTO SoP11PreBats (TournamentID, Player, Points) VALUES 
(15, 'Jo',  10), 
(14, 'Jo',  20), 
(13, 'Jo',  30), 
(12, 'Jo',  40), 
(11, 'Jo',  50), 
(10, 'Jo',  60), 
(9, 'Jo',  70), 
(8, 'Jo',  80), 
(7, 'Jo',  90), 
(6, 'Jo', 100), 
(5, 'Jo', 110), 
(4, 'Jo', 120), 
(3, 'Jo', 130), 
(2, 'Jo', 140), 
(1, 'Jo', 150), 
(1, 'Tel', 15), 
(2, 'Tel', 25), 
(3, 'Tel', 35), 
(4, 'Tel', 45), 
(5, 'Tel', 55), 
(6, 'Tel', 65), 
(7, 'Tel', 75), 
(8, 'Tel', 85), 
(9, 'Tel', 95), 
(10, 'Tel', 105), 
(11, 'Tel', 115), 
(12, 'Tel', 125), 
(13, 'Tel', 135), 
(14, 'Tel', 145), 
(15, 'Tel', 155), 
(1, 'Kevin', 10), 
(2, 'Kevin', 20), 
(3, 'Kevin', 30), 
(4, 'Kevin', 40), 
(5, 'Kevin', 50), 
(6, 'Kevin', 60), 
(7, 'Kevin', 70), 
(8, 'Kevin', 80), 
(9, 'Kevin', 90), 
(10, 'Kevin', 100), 
(11, 'Kevin', 110), 
(12, 'Kevin', 120), 
(13, 'Kevin', 130), 
(14, 'Kevin', 140), 
(15, 'Kevin', 150); 

結果:

+--------+--------+-----------+ 
| Player | Points | MinPoints | 
+--------+--------+-----------+ 
| Jo  | 1050 |  60 | 
| Kevin | 1050 |  60 | 
| Tel | 1100 |  65 | 
+--------+--------+-----------+ 
相關問題