将一行与整个字段进行比较

时间:2012-08-16 10:44:53

标签: sql ms-access

我有一张高尔夫结果表,类似如下:

name           r1    r2    r3    r4    event
Tiger Woods    71    68    67    72    Buick Invitational 2006
Nathan Green   67    70    69    72    Buick Invitational 2006
J.M. Olazabal  74    64    71    69    Buick Invitational 2006
Arjun Atwal    70    67    71    71    Buick Invitational 2006

我希望能够计算出每个球员在给定的一天/每轮比赛中得分优于对手的频率。

例如在上面的例子中,在所示的四名球员中,老虎的得分低于J.M. Olazabal第一轮1/3 = 33%。

第三轮比赛得分最低,100%。

Arjun Atwal在r3中得分最高,击败没有人,所以= 0%。

等等......

基本上我想检查每一行与事件字段相同的每一行。任何帮助非常感谢!

编辑:为了清楚起见,我想要的输出看起来如下:

Tiger Woods    33%    33%    100%   0%     Buick Invitational 2006
Nathan Green   100%   0%     67%    0%     Buick Invitational 2006
J.M. Olazabal  0%     100%   0%     100%   Buick Invitational 2006
Arjun Atwal    67%    67%    0%     67%    Buick Invitational 2006

再次感谢。

2 个答案:

答案 0 :(得分:0)

因为我认为它可能很有趣,所以这里的SQL将为您提供您描述的确切结果:

SELECT t.name, 
Format((Select Count(*) from [Table] where [Table].r1 > T.r1 And [Table].event = t.event)/3,"0%") as r1,
Format((Select Count(*) from [Table] where [Table].r2 > T.r2 And [Table].event = t.event)/3,"0%") as r2, 
Format((Select Count(*) from [Table] where [Table].r3 > T.r3 And [Table].event = t.event)/3, "0%") as r3, 
Format((Select Count(*) from [Table] where [Table].r4 > T.r4 And [Table].event = t.event)/3, "0%") as r4,
event
FROM [Table] as T;

答案 1 :(得分:0)

如果您的数据存储在名为Table1的MS Access表中,则下面的SQL将完成此操作。您可以通过使用count(*) - 1而不是3来消除假设您有4个竞争对手。

将以下内容粘贴到新查询的SQL视图中。

SELECT T.name, Sum(IIf(T2.r1>T.r1,1,0)*100/3), Sum(IIf(T2.r2>T.r2,1,0)*100/3), Sum(IIf(T2.r3>T.r3,1,0)*100/3), Sum(IIf(T2.r4>T.r4,1,0)*100/3)
FROM Table1 AS T, Table1 AS T2
GROUP BY T.name;