计算满足条件的行数

发布于 2024-12-13 19:25:33 字数 351 浏览 3 评论 0原文

我真的认为我的问题不太具体,我只是不知道如何解释。

假设我有一张包含以下列

团队 | 的 表格游戏类型 |日期 |结果

我想创建一个视图,列出有关团队的一些统计信息

SELECT team, count(*) as FROM matches GROUP BY team ORDER BY Total DESC

现在我想添加计数列 胜负

  • 联赛比赛总数
  • 过去一个月的
  • 平局
  • 目的

是在数据库中创建一个视图,而不是任何新的表或列

I really think the question I have is not too specific, I just don't know how to explain it.

Say I have a table with following Collumns

team | gameType | date | outcome

I would like to create a view that lists some statistics about the teams

SELECT team, count(*) as FROM matches GROUP BY team ORDER BY total DESC

Now I want to add columns counting the total

  • which were league games
  • which have been in the past month
  • wins
  • losses
  • ties

The purpose is to create a view in the database, not any new tables or columns.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨与橙与城 2024-12-20 19:25:33

您可以在聚合函数中使用 case 语句来仅聚合特定事件。

SELECT 
    team, 
    count(*) total_matches,
    count(case when gameType = 'league' then 1 end) league_matches,
    count(case then date > now() - INTERVAL 30 DAY then 1 end) recent_matches,
    ...
FROM matches 
GROUP BY team

You can use case statements in your aggregation functions to aggregate only for specific events.

SELECT 
    team, 
    count(*) total_matches,
    count(case when gameType = 'league' then 1 end) league_matches,
    count(case then date > now() - INTERVAL 30 DAY then 1 end) recent_matches,
    ...
FROM matches 
GROUP BY team
悟红尘 2024-12-20 19:25:33

总体思路是在聚合内部使用 CASE 表达式

SELECT
    team,
    count(*) as total,
    count(CASE WHEN outcome = 'WIN' THEN 1 END) as wins,
    count(CASE WHEN outcome = 'DRAW' THEN 1 END) as draw,
    count(CASE WHEN outcome = 'LOSE' THEN 1 END) as losses,
    count(CASE WHEN gametype = 'League' THEN 1 END) as leaguegames,
    count(CASE WHEN DATE_SUB(CURDATE(), INTERVAL 1 MONTH) < dateTHEN 1 END) as lastmonth
FROM matches
GROUP BY team 
ORDER BY total DESC

The general idea is to use CASE expression inside the aggregate

SELECT
    team,
    count(*) as total,
    count(CASE WHEN outcome = 'WIN' THEN 1 END) as wins,
    count(CASE WHEN outcome = 'DRAW' THEN 1 END) as draw,
    count(CASE WHEN outcome = 'LOSE' THEN 1 END) as losses,
    count(CASE WHEN gametype = 'League' THEN 1 END) as leaguegames,
    count(CASE WHEN DATE_SUB(CURDATE(), INTERVAL 1 MONTH) < dateTHEN 1 END) as lastmonth
FROM matches
GROUP BY team 
ORDER BY total DESC
兔姬 2024-12-20 19:25:33

比使用 CASE 更简单、更易读(至少对我来说)是使用 OR:

SELECT
   team,
   count(*) as total,
   count(outcome = 'WIN'  OR NULL) as wins,
   count(outcome = 'DRAW' OR NULL) as draw,
   ...
FROM matches
GROUP BY team

Somewhat simpler and more readable (to me at least) than using CASE is using OR:

SELECT
   team,
   count(*) as total,
   count(outcome = 'WIN'  OR NULL) as wins,
   count(outcome = 'DRAW' OR NULL) as draw,
   ...
FROM matches
GROUP BY team
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文