2017-10-12 7 views
1

私は2つのテーブルを結合しようとしています。完成したクエリは以下の通りです:私のSQLクエリが結果を2倍にする理由

select 
    cast(M.Rank as signed) as Rank, 
    O.Country, 
    O.Continent, 
    M.Gold, 
    M.Silver, 
    M.Bronze, 
    M.Total 
from 

(
select @rownum := @rownum + 1 as RankStd, 
    CASE 
     WHEN (@Gold=T.Gold and @Silver=T.Silver and @Bronze=T.Bronze) 
     THEN @rank := @rank 
     ELSE @rank := @rownum 
    END as Rank, 



     (@Gold:=T.Gold) Gold, 
     (@Silver:=T.Silver) Silver, 
     (@Bronze:=T.Bronze) Bronze, 
     T.Total, 
     T.City, 
     T.Season 

from 

(


select 
    sum(Gold) as Gold, 
    sum(Silver) as Silver, 
    sum(Bronze) as Bronze, 
    sum(Total) as Total, 
    City, 
    Season 
from 
(
    select 
    City, 
    Season, 
    case when Medal ='gold' then 1 else 0 end as Gold, 
    case when Medal ='silver' then 1 else 0 end as Silver, 
    case when Medal ='bronze' then 1 else 0 end as Bronze, 
    1 as Total 
    from MedalResults 
    where Country = 'ITA' 
    and Season='Summer' 


) a 

group by City 

) T 

    CROSS JOIN (SELECT @rownum := 0, 
         @Gold := 0, 
         @Silver := 0, 
         @Bronze := 0, 
         @rank := 1) v 

    order by Gold desc, Silver DESC, Bronze DESC 

)M 

join OrgCountry O 
on O.City = M.City and O.Season=M.Season 

order by Rank 

Mの表は、この構造(私は最初の行を記述)と私のテーブルを取得します。

RankStd Rank Gold Silver Bronze Total City  Season 
----------------------------------------------------------- 
1  1 26 18  24  68 Los Angeles Summer 
2  2 19 23  21  63 London  Summer 
3  3 17 17  14  48 Athina  Summer 
4  4 14 6  5  25 Antwerpen  Summer 
5  5 13 10  13  36 Roma   Summer 
6  6 13 10  12  35 Atlanta  Summer 
OrgCountry表が実際にこのような構造を有している

(I )明らかに1996年から1984年の間、他のエントリがあるが、この例のために、私はそれを切り取って、あなたが理解するためにのみsignificative行を記述します。

ID Year City  Country  Continent Season 
----------------------------------------------------- 
1 2012 London  Great Britain Europe  Summer 
2 2008 Beijing  China   Asia  Summer 
3 2004 Athina  Greece   Europe  Summer 
4 2000 Sydney  Australia  Oceania  Summer 
5 1996 Atlanta  United States Americas Summer 
6 1984 Los Angeles United States Americas Summer 

私はのようにそれらの間の結合を行った場合上記のように結果は2倍になります。

Rank Country  Continent Gold Silver Bronze Total 
------------------------------------------------------------- 
1  United States Americas 26  18  24  68 
1  United States Americas 26  18  24  68 
2  Great Britain Europe  19  23  21  63 
2  Great Britain Europe  19  23  21  63 
2  Great Britain Europe  19  23  21  63 
3  Greece   Europe  17  17  14  48 
3  Greece   Europe  17  17  14  48 
3  Greece   Europe  17  17  14  48 

これはなぜですか?私が欲しいのは、単に国のために集計して得ることです:

Rank Gold Silver Bronze Total Country  Season 
----------------------------------------------------------- 
1 39 28  36  113  United States Summer 
2 19 23  21  63  Great Britain Summer 
3 17 17  14  48  Greece   Summer 
4 14 6  5  25  Netherlands Summer 
5 13 10  13  36  Italy   Summer 
6 13 10  12  35  Australia  Summer 

などです。 アトランタとロサンゼルスは米国の都市であるため、最初の行が変更されています。

+0

がhttps://meta.stackoverflow.com/questions/333952/を参照してください。なぜ、私が提供する、何のためのものなのか、非常にシンプルなSQLクエリー – Strawberry

+0

私はこのページを追加しました:http://sqlfiddle.com/# !9/97ec5c/1 – Idro

+0

それで、各組織国でイタリアが獲得した金メダル、銀メダル、ブロンズメダルの数を調べようとしていますか? – Strawberry

答えて

0

質問の要点は次のとおりです。あなたのsqlfiddleはさらに答えるために、問題の十分な代表ではありません。

SELECT y.country 
    , x.medal 
    , COUNT(*) subtotal 
    FROM medalresults x 
    JOIN (SELECT DISTINCT city, country FROM orgcountry) y 
    ON y.city = x.city 
WHERE x.country = 'ITA' 
GROUP 
    BY country 
    , medal; 

...または多分...

SELECT y.country 
    , SUM(CASE WHEN medal = 'gold' THEN 1 ELSE 0 END) gold 
    , SUM(CASE WHEN medal = 'silver' THEN 1 ELSE 0 END) silver 
    , SUM(CASE WHEN medal = 'bronze' THEN 1 ELSE 0 END) bronze 
    FROM medalresults x 
    JOIN (SELECT DISTINCT city, country FROM orgcountry) y 
    ON y.city = x.city 
WHERE x.country = 'ITA' 
GROUP 
    BY country; 
関連する問題