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

Sql 将一行与整个字段进行比较,sql,ms-access,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 Ar

我的高尔夫成绩表如下所示:

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
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
我希望能够以percetanges为单位计算出每名球员在给定的一天/一轮中得分高于对手的频率

例如,在上面所示的四名球员中,老虎队在第一轮中的得分仅低于J.M.奥拉扎巴尔。1/3=33%

在第三轮,他得分最低,所以100%

Arjun Atwal在r3中得分最高,击败任何人,因此=0%

等等

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

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

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
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

再次感谢。

因为我认为这可能很有趣,下面是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;

如果您的数据存储在一个名为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;

你能展示一下你最初的解决方案吗?老实说,我甚至不知道从哪里开始。我猜解决方案可能需要使用无数的IIf语句。恐怕我对这件事还不太熟悉!如果将数据标准化,这类事情就会变得容易得多。