-1

我正在从事学校体育项目..在这个我有一张桌子:-结果(状态,点)

对于奖牌,我有以下标准:-

Gold  - 7 Point
Silver- 5 Points
Bronze- 4 Points

我想计算每个州获得的金牌、银牌和铜牌总数。为此,我使用了以下查询:

select STATE,sum(Point) as MEDAL from [Sheet2$] Group by STATE order by sum(Point) desc

但它只计算总分。我也想要金牌、银牌和铜牌的总数。所以请帮帮我

4

3 回答 3

3

您可以尝试做一些条件逻辑 - 我在这里假设您没有其他列,并且数据尚未以任何方式聚合,但是,可能类似于:

SELECT      STATE,
            SUM(Point) AS MEDAL,
            SUM(CASE WHEN Point = 7 THEN 1 ELSE 0 END) AS GoldCount,
            SUM(CASE WHEN Point = 5 THEN 1 ELSE 0 END) AS SilverCount,
            SUM(CASE WHEN Point = 4 THEN 1 ELSE 0 END) AS BronzeCount

FROM        [Sheet2$]

GROUP BY    STATE 

ORDER BY    SUM(Point) DESC
于 2013-09-20T07:48:33.693 回答
0

如果我理解正确,您将根据表中的点数确定奖牌,然后您可以将该结果分组以计算奖牌:

select
  STATE, Medal, count(*) as [Count]
from
  (select
    STATE,
    case
      when Point >= 7 then 'Gold'
      when Point >= 5 then 'Silver'
      when Point >= 4 then 'Bronze'
      else null
    end as Medal
  from
    [Sheet2$]
  ) x
group by
  STATE, Medal
order by
  STATE
于 2013-09-20T07:51:40.873 回答
0

这是其他策略的替代方案。假设您的表格如下所示:

+-------+--------+
| state | points |
+-------+--------+
| IL    |      7 |
| ON    |      3 |
| ON    |      7 |
...
| TX    |      7 |
+-------+--------+

然后这样的事情会得到奖牌数:

SELECT
  state,
  count(*) AS medal
FROM your_table
GROUP BY state, points
ORDER BY state, points desc
于 2013-09-20T07:51:49.810 回答