如何使用单个查询列出单个字段的聚合枚举值?

时间:2012-08-17 03:48:03

标签: sql aggregate-functions enumeration

标题有点令人费解。这是一个具体的例子。我有两张桌子:

+-------------+--------------------------------+------+-----+---------+-------+
| Field       | Type                           | Null | Key | Default | Extra |
+-------------+--------------------------------+------+-----+---------+-------+
| event       | varchar(100)                   | NO   | MUL | NULL    |       |
| sport       | varchar(100)                   | NO   |     | NULL    |       |
| athleteCode | char(10)                       | NO   | MUL | NULL    |       |
| medal       | enum('GOLD','SILVER','BRONZE') | NO   |     | NULL    |       |
+-------------+--------------------------------+------+-----+---------+-------+

+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| name    | varchar(100) | NO   |     | NULL    |       |
| code    | char(10)     | NO   | PRI | NULL    |       |
| country | varchar(100) | NO   | MUL | NULL    |       |
+---------+--------------+------+-----+---------+-------+

第一张表是奖牌表。第二张桌子是运动员桌子。这两个表格通过medals.athleteCodeathlete.code相关联。我希望能够列出显示以下信息的查询:

COUNTRY | GOLD | SILVER | BRONZE | TOTAL

到目前为止,我能够做到的唯一方法是使用此查询:

SELECT country, medal, COUNT(medal) as count  
    FROM athletes, medals  
    WHERE athletes.code=medals.athleteCode  
    GROUP BY country, medal  
    ORDER BY country, medal;

但在我执行此查询之后,我仍然需要处理查询(通过PHP),因为这只能让我获得每个国家的奖牌类型(即所有中国的金牌,所有中国的银牌,所有中国的铜牌等)。有没有办法创建查询,其中查询的每个记录(即行)是:COUNTRY | GOLDS | SILVERS | BRONZES | TOTAL?我看了COUNT(),但我不确定如何使用它。


2012年8月17日

@“habib zare”的解决方案非常接近。这是我对它的调整:

SELECT country, 
(SELECT count(*) FROM medals WHERE a.code=m.athleteCode AND medal='Gold') AS Gold,
(SELECT count(*) FROM medals WHERE a.code=m.athleteCode AND medal='Silver') AS Silver,
(SELECT count(*) FROM medals WHERE a.code=m.athleteCode AND medal='Bronze') AS Bronze,
(SELECT count(*) FROM medals WHERE a.code=m.athleteCode) AS Total
FROM medals m JOIN athletes a
ON m.athleteCode=a.code
GROUP BY country
ORDER BY country, Gold DESC, Silver DESC, Bronze DESC

问题是辅助SELECT语句需要根据国家/地区进行选择;那就是我需要的东西:

SELECT country AS Country,
(SELECT count(*) FROM medals WHERE a.code=m.athleteCode AND medal='Gold' AND a.country=Country) AS Gold,

3 个答案:

答案 0 :(得分:0)

在MS SQL Server SUM(CASE WHEN medal = 'GOLD' THEN 1 ELSE 0 END) AS GOLDS中可以与GROUP BY country一起使用。

答案 1 :(得分:0)

如果您使用的是MSSQL,也可以使用OUTER APPLY:

样品:

SELECT Country
  ,Gold = ISNULL(GoldMedals.MedalCount,0)
  ,Total = ISNULL(TotalMedals.MedalCount,0) 
FROM athletes
  OUTER APPLY (
     SELECT MedalCount = COUNT(*)
     FROM medals
     WHERE medals.athletescode = atheletes.medalsathleteCode
       AND medals.Type = 'Gold'
   ) GoldMedals
   OUTER APPLY (
     SELECT MedalCount = COUNT(*)
     FROM medals
     WHERE medals.athletescode = atheletes.medalsathleteCode
    ) TotalMedals   
GROUP BY country

您可能需要添加SUM而不是ISNULL,但我没有真正测试此代码,因此请按原样使用...

答案 2 :(得分:0)

我不明白你想要什么,但我想你想要这个:

奖牌: 奖牌赛事体育运动员代码 金jh游泳1

Bronze dfg ert 2

Gold fg err 1

Silver as erf 4

青铜如erf 5

黄金df dfg 6

Gold sdds tekvando 3

青铜df jh 1

青铜yy jh 1

Silver ik as 1

Silver shj jsg 3

Silver shj jsg 5

Silver sjdk hgj 5

Silver wuytu wopow 5

Silver wuytu wopow 6

Silver wuytu wopow 6

运动员:

名称代码国家/地区

habib 1 iran

ahmad 2 azerbaijan

mehmad 3 turkey

所以4伊朗

ghg 5 azerbaijan

yewtuuy 6阿塞拜疆

我的疑问:

SELECT country,name,
(select count(*) from medals where athleteCode=m.athleteCode and medal='Gold') as Gold,
(select count(*) from medals where athleteCode=m.athleteCode and medal='Silver') as Silver,
(select count(*) from medals where athleteCode=m.athleteCode and medal='Bronze') as Bronze,
(select count(*) from medals where athleteCode=m.athleteCode) as Total
FROM medals m join athletes a
on m.athleteCode=a.code
group by country,code
order by country,Gold desc,Silver desc,Bronze desc

结果:

国家/地区名称Gold Silver Bronze Total

阿塞拜疆yewtuuy 1 2 0 3

阿塞拜疆ghg 0 3 1 4

阿塞拜疆艾哈迈德0 0 1 1

iran habib 2 1 2 5

伊朗所以0 1 0 1

火鸡mehmad 1 1 0 2