SQL:从相同的表/字段计数,但值不同

时间:2013-06-02 19:34:49

标签: sql subquery

在我的数据库项目中,我不得不建立一个关于奥运会数据的数据库。

我还必须构建以下查询:

为用户提供的特定奥运会计算奖牌表。奖牌表应包含 国家的国际奥委会代码,后面是金牌,银牌,铜牌和总奖牌数。应该首先 按金币数量排序,然后是银币,最后是青铜器。

基本上,我有一张桌子Medals,其中包含一些参赛者在一些奥运会比赛中赢得的奖牌。

奖牌以下列方式存储:“金牌”,“银牌”,“铜牌”位于我的color字段中Medals

我尝试使用以下查询:

SELECT q1.country, q1.name as "Game", q1.cntG, q2.cntS, q3.cntB FROM
(
    SELECT c.countryName as country, g.name as name, count(m.idMedal) as cntG 
    FROM Game g
    INNER JOIN Participant p
    ON p.fkGame = g.idGame
    INNER JOIN Country c
    ON p.fkCountry = c.idCountry
    INNER JOIN Medals m
    ON m.fkMedalist = p.idParticipant
    WHERE
    g.name = "2012 Summer Olympics" 
    AND m.color like '%Gold%'
    GROUP BY c.countryName
    ORDER BY c.countryName, cntG DESC
) as q1,
(
    SELECT c.countryName as country, g.name as name, count(m.idMedal) as cntS 
    FROM Game g
    INNER JOIN Participant p
    ON p.fkGame = g.idGame
    INNER JOIN Country c
    ON p.fkCountry = c.idCountry
    INNER JOIN Medals m
    ON m.fkMedalist = p.idParticipant
    WHERE g.name = "2012 Summer Olympics" 
    AND m.color like '%Silver%'
    GROUP BY c.countryName
    ORDER BY c.countryName, cntS DESC
) as q2,
(
    SELECT c.countryName as country, g.name as name, count(m.idMedal) as cntB 
    FROM Game g
    INNER JOIN Participant p
    ON p.fkGame = g.idGame
    INNER JOIN Country c
    ON p.fkCountry = c.idCountry
    INNER JOIN Medals m
    ON m.fkMedalist = p.idParticipant
    WHERE g.name = "2012 Summer Olympics" 
    AND m.color like '%Bronze%'
    GROUP BY c.countryName
    ORDER BY c.countryName, cntB DESC
) as q3
GROUP BY q1.country
ORDER BY q1.cntG, q2.cntS, q3.cntB DESC

嗯,它给了我一个非常奇怪的结果。我知道这个查询有问题,但无法弄清楚它是什么!

希望你能帮助我:)。

由于

注意:我暂时忽略了查询总数(在分配中提出)。一旦我想到构建第一部分,我将尝试总计

3 个答案:

答案 0 :(得分:3)

数据不是问题 - 在q1,q2和q3 中的每一个之间存在隐式笛卡尔连接这一事实是一个问题。尝试:

SELECT c.countryName as country, 
       count(case m.idMedal when 'Gold medal' then 1 end) as cntG,
       count(case m.idMedal when 'Silver medal' then 1 end) as cntS,
       count(case m.idMedal when 'Bronze medal' then 1 end) as cntB
FROM Game g
INNER JOIN Participant p ON p.fkGame = g.idGame
INNER JOIN Country c ON p.fkCountry = c.idCountry
INNER JOIN Medals m ON m.fkMedalist = p.idParticipant
WHERE g.name = "2012 Summer Olympics" 
GROUP BY c.countryName
ORDER BY cntG DESC, cntS DESC, cntB DESC

答案 1 :(得分:2)

很抱歉,但你的查询真是一团糟。你不能简单地以这种方式加入3个子查询。

但是,您可以在一个查询中拥有所需的内容。我会给你伪代码并留下你的详细信息:)

SELECT
    [countryName],
    SUM(color like '%Gold%') as total_gold,
    SUM(color like '%Silver%') as total_silver,
    SUM(color like '%Bronze%') as total_bronze,
    COUNT(*) as total
FROM Medals
INNER JOIN Participant (...)
INNER JOIN Country (...)
INNER JOIN Game (...)
WHERE (...)
GROUP BY [countryName]
ORDER BY total_gold DESC, total_silver DESC, total_bronze DESC;

答案 2 :(得分:1)

有很多方法可以修复甚至改进您的查询,其中一种方法是添加

 ...
 WHERE q1.country = q2.country 
   AND q1.country = q3.country

(就在GROUP BY q1.country之前)。

另一个解决方案是在country上加入q1,q2和q3。