以下是我的数据透视查询。
Select MID, [1] as AthleteName1st,
[2] as AthleteName2nd,
[3] as AthleteName3rdPoint
from(
select MId, AthleteId , Position,
(row_number() over(partition by Position ,R.EventCode,MId order by Position)) rn
from L_Results R
where R.EventCode = '040' and R.Season = (select Season from L_SeasonTable) and Position in (1,2,3)
) src
pivot
(
max([AthleteId])
for Position in ([1] ,[2] , [3])
) piv
结果我得到:
MID AthleteName1st AthleteName2nd AthleteName3rdPoint
65 14231808 14201894 14202005
122 14238588 14238562 14186000
178 14238588 14231808 14201894
234 14201847 14201894 14367591
346 14201847 14202176 14186000
374 14201847 14202176 14201912
402 14202176 14249856 14201894
402 14201848 NULL NULL
结果要求为:
MID AthleteName1st AthleteName2nd AthleteName3rdPoint
65 14231808 14201894 14202005
122 14238588 14238562 14186000
178 14238588 14231808 14201894
234 14201847 14201894 14367591
346 14201847 14202176 14186000
374 14201847 14202176 14201912
402 14202176, 14249856 14201894
14201848
与在数据透视查询中一样,我从 L_Results 表中仅检索第 1、第 2 和第 3 个位置的数据,在 MID '402' 的 L_Results 表中,对于第一个位置,我有 2 条记录,即为什么我在 MID 的结果中获得 2 条记录402'.. 在上述情况下,我需要得到如上所示的结果,MID '402' 只有一条记录。
我非常感谢您的帮助...谢谢