0

我必须弄清楚哪些游泳者在同一事件中获得了相同的位置。对于每一对这样的游泳者,我必须显示一行,显示每个游泳者的事件 ID、地点和参赛者编号。例子

eventid  place  competitors tied

SWM012  2   1072528 1099641

SWM021  2   1018529 1061698

到目前为止,这是我的代码

select R1.Eventid, R1.Place, R1.Competitornum, R2.Competitornum
from Results R1, Results R2 
where R1.Place = R2.Place
group by R1.Eventid, R1.Place, R1.Competitornum, R2.Competitornum

这是输出

eventid  Place   competitornum    competitornum

SWM010   1       1121587           1056740

SWM010   1       1121587           1061698

依此类推,第一列的相同竞争者编号在第二个竞争者列中不同

我只想知道如何获得上述描述中的输出

4

1 回答 1

0

首先,您可能还需要加入 Event ID

select R1.Eventid, R1.Place, R1.Competitornum, R2.Competitornum
from Results R1, Results R2 
where R1.Place = R2.Place AND R1.EventId = R2.EventId
group by R1.Eventid, R1.Place, R1.Competitornum, R2.Competitornum

上述查询返回与此相同的结果

select distinct R1.Eventid, R1.Place, R1.Competitornum, R2.Competitornum
from Results R1, Results R2 
where R1.Place = R2.Place AND R1.EventId = R2.EventId

但这将返回所有独特的“关系”对。一个解决方案是这样

select distinct R1.Eventid, R1.Place, R1.Competitornum, R2.Competitornum
from Results R1, Results R2 
where R1.Place = R2.Place AND R1.EventId = R2.EventId AND R1.Competitornum > R2.Competitornum

不过,这仅适用于双向联系。

于 2012-08-30T04:09:46.883 回答