我正在尝试在两个表之间建立连接。完成的查询如下:
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表确实有这样的结构(我只写有意义的行让你明白,显然在1996年和1984年之间还有其他条目,但是对于这个例子我把它剪掉了):
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
如果我像上面那样在他们之间进行连接,我得到的结果加倍:
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 :(得分: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;