我必须弄清楚哪些游泳运动员在同一场比赛中获得了相同的位置。对于每对这样的游泳运动员,我必须显示一行,显示每个游泳运动员的比赛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
以及第二列竞争对手栏中第一列不同的竞争对手
我只是想知道如何获得输出,如上面的描述
答案 0 :(得分:0)
首先,您可能还需要加入事件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
这只适用于双向联系。