使用 SQL 聚合和计算统计数据

发布于 2024-12-06 05:32:33 字数 726 浏览 0 评论 0原文

我有一款射击游戏,用户在一周的时间内相互竞争以积累最多的分数。我想编写一个查询来聚合镜头表中的统计数据。这里关注的表和关系是:

  • user has much opportunity_competition_period
  • 属于用户
  • co​​mpetition_period有很多镜头
  • shot属于competition_period

在shots表中,我有以下字段可以使用:

  • result -->;字符串值:WON、LOST 或 TIED
  • amount_won -->整数值:例如-100、0、2000等。

对于每个用户,我想返回具有以下聚合统计信息的结果集:

  • won_countlost_counttied_counttotal_shots_count
  • total_amount_won
  • ( amount_won
  • (won_count+lost_count+tied_count)
  • 的总和)
  • avg_amount_won_per_shot( Total_amount_won /total_shots_count)

我已经工作了这个查询已经有几个小时了,但还没有取得多大进展。统计功能让我很困惑。一位朋友建议我尝试将结果返回到一个名为 shot_records 的新虚拟表中。

I have shoot 'em game where users compete against each other over the course of a week to accumulate the most points. I want to write a query that aggregates statistical data from the shots table. The tables and relationships of concern here are:

  • user has many competition_periods
  • competition_period belongs to user
  • competition_period has many shots
  • shot belongs to competition_period

In the shots table I have the following fields to work with:

  • result --> string values: WON, LOST or TIED
  • amount_won --> integer values: e.g., -100, 0, 2000, etc.

For each user, I want to return a result set with the following aggregated stats:

  • won_count
  • lost_count
  • tied_count
  • total_shots_count (won_count + lost_count + tied_count)
  • total_amount_won (sum of amount_won)
  • avg_amount_won_per_shot (total_amount_won / total_shots_count)

I've worked on this query for few hours now, but haven't made much headway. The statistical functions trip me up. A friend suggested that I try to return the results in a new virtual table called shot_records.

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

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

发布评论

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

评论(1

口干舌燥 2024-12-13 05:32:33

这是基本的解决方案,计算给定球员的所有击球的统计数据(您没有指定是否希望在每个比赛周期的基础上进行统计):

 SELECT user, SUM(IF(result = 'WON', 1, 0))  AS won_count, 
              SUM(IF(result = 'LOST', 1, 0)) AS lost_count, 
              SUM(IF(result = 'TIED', 1, 0)) AS tied_count, 
              COUNT(*)                       AS total_shots_count, 
              SUM(amount_won)                AS total_amount_won, 
              (SUM(amount_won) / COUNT(*))   AS avg_amount_won_per_shot 
 FROM user U INNER JOIN competition_periods C ON U.user_id = C.user_id
 INNER JOIN shots S ON C.competition_period_id = S.competition_period_id
 GROUP BY user

请注意,这包括计算“总胜利”时的负数数字(即总数减去损失)。如果这不是您游戏的正确算法,您可以将两个地方的 SUM(Amount) 更改为 SUM(IF(Amount > 0, Amount, 0))出现在查询中。

Here is the basic solution, computing the statistics across all shots for a given player (you didn't specify if you want them on a per-competition-period basis or not):

 SELECT user, SUM(IF(result = 'WON', 1, 0))  AS won_count, 
              SUM(IF(result = 'LOST', 1, 0)) AS lost_count, 
              SUM(IF(result = 'TIED', 1, 0)) AS tied_count, 
              COUNT(*)                       AS total_shots_count, 
              SUM(amount_won)                AS total_amount_won, 
              (SUM(amount_won) / COUNT(*))   AS avg_amount_won_per_shot 
 FROM user U INNER JOIN competition_periods C ON U.user_id = C.user_id
 INNER JOIN shots S ON C.competition_period_id = S.competition_period_id
 GROUP BY user

Note that this includes negatives in calculating the "total won" figure (that is, the total is decreased by losses). If that's not the correct algorithm for your game, you would change SUM(Amount) to SUM(IF(Amount > 0, Amount, 0)) in both places it occurs in the query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文