计算查询中的人数

时间:2016-08-04 09:26:29

标签: sql ms-access ms-access-2010

我有一个查询,可以找到体育人士参加比赛的最佳时间等。我的查询基本上会过滤掉所有比赛,而不是个人最佳时间。

我想展示他们在比赛中完成比赛的其他人的比赛次数

我无法理解如何做到这一点,所以希望有人可以提供帮助,这是我的代码:

SELECT tblSourceData.Athlete, Max(tblSourceData.Swim) AS MaxOfSwim, Max(tblSourceData.Bike) AS MaxOfBike, Max(tblSourceData.Run) AS MaxOfRun, Count(tblSourceData.Athlete) AS EventsCompleted
    FROM tblSourceData
    WHERE (((tblSourceData.Gender)="f"))
    GROUP BY tblSourceData.Athlete
    HAVING (((Count(tblSourceData.Athlete))>=5));

我桌上的我的行是:

Athlete
Swim
Run
Bike
Date
Gender

--------从评论中添加更多信息-----------

此刻我的查询显示了这样的例子:

Name      Swim      Bike       Run       CompletedEvents       %
Dave      43:22    11:23      32:34            46
John      43:23    31:24      11:43            12

我想知道所有参加比赛的运动员的平均百分比

提前谢谢

2 个答案:

答案 0 :(得分:0)

试试这个:

Array( [left] => 12342    [right] => 43244 )

希望它有所帮助!

答案 1 :(得分:0)

如果您想要的是每个运动员在所有比赛中完成的比赛的百分比,您只需要使用SUM()来总结所有已完成的比赛,然后计算每个用户的百分比。我无法测试它但是这样应该有效:

SELECT tblSourceData.Athlete as Athlete,
       Max(tblSourceData.Swim) AS MaxOfSwim,
       Max(tblSourceData.Bike) AS MaxOfBike, 
       Max(tblSourceData.Run) AS MaxOfRun,
       Count(tblSourceData.Athlete) AS EventsCompleted,
       ROUND((100*Count(tblSourceData.Athlete)/(SELECT SUM(b.eventsCompleted) FROM  (      
           SELECT COUNT(tblSourceData.Athlete) AS EventsCompleted FROM tblSourceData
           WHERE tblSourceData.Gender="f"
           GROUP BY tblSourceData.Athlete
           HAVING Count(tblSourceData.Athlete)>=5
       ) AS b)),2)  AS `percentage` 
       FROM tblSourceData
       WHERE tblSourceData.Gender="f"
       GROUP BY tblSourceData.Athlete
       HAVING Count(tblSourceData.Athlete)>=5