交叉应用表值函数

时间:2015-07-29 19:57:34

标签: sql sql-server sql-server-2008-r2

这是一个真正的思想家伙!

我有一张表基本上将用户定位在一个联盟中:

LeagueID Stake  League_EntryID  UserID  TotalPoints TotalBonusPoints    Prize
13028   2.00        58659        2812       15           5              NULL
13028   2.00        58662        3043       8            3              NULL
13029   5.00        58665        2812       8            3              NULL

League_EntryID是此处的唯一字段,但您会看到此查询返回当天输入的多个联赛。

我还有一个表值函数,它返回联盟的当前奖励积分,并接受LeagueID作为参数并返回有资格获得奖金的人。这是一个复杂的函数,理想情况下我想保留作为接受LeagueID的函数。结果如下:

UserID  Position    League_EntryID  WinPerc     Prize
2812    1               58659       36.000000   14.00
3043    6               58662       2.933333    4.40
3075    6               58664       2.933333    4.40

基本上我想要做的是通过传入LeagueID来将表值函数连接到最顶层的查询,以基本上更新该League_EntryID的奖励字段,即

SELECT * FROM [League]
INNER JOIN [League_Entry] ON [League].[LeagueID] = [League_Entry].[LeagueID]
INNER JOIN [dbo].[GetPrizesForLeague]([League].[LeagueID]) ....

我不确定CROSS APPLY是否可以在这里工作,但基本上我相信我需要加入LeagueID和League_EntryID来为我提供奖品的价值。不知道如何在没有访问标量函数的情况下执行此操作,该标量函数将依次调用表值函数并从中获取奖励。

速度令我担心。

P.S。并非所有的League_EntryID都将作为表值函数输出的一部分存在,因此可以使用OUTER JOIN / APPLY吗?

编辑请参阅下面的查询

SELECT DISTINCT [LeagueID],
    [CourseName],
    [Refunded],
   [EntryID],
   [Stake],
   d.[League_EntryID],
   d.[UserID],
   [TotalPoints],
   [TotalBonusPoints],
   [TotalPointsLastRace],
   [TotalBonusPointsLastRace],
   d.[Prize],
   [LeagueSizeID],
   [TotalPool],
   d.[Position],
   [PositionLastRace],
   t.Prize

FROM
(
SELECT [LeagueID],
   [EntryID],
   [Stake],
   [MeetingID],
   [Refunded],
   [UserID],
   [League_EntryID],
   [TotalPoints],
   [TotalBonusPoints],
   [TotalPointsLastRace],
   [TotalBonusPointsLastRace],
   [Prize],
   [LeagueSizeID],
   [dbo].[GetTotalPool]([LeagueID], 1) AS [TotalPool],
   RANK() OVER( PARTITION BY [LeagueID] ORDER BY [TotalPoints] DESC, [TotalBonusPoints] DESC) AS [Position],
   RANK() OVER( PARTITION BY [LeagueID] ORDER BY [TotalPointsLastRace] DESC, [TotalBonusPointsLastRace] DESC) AS [PositionLastRace],
   ROW_NUMBER() OVER (PARTITION BY [LeagueID]
                                ORDER BY [TotalPoints] DESC, [TotalBonusPoints] DESC
                               ) as [Position_Rownum]
FROM [DATA] ) AS d

INNER JOIN [Meeting] WITH (NOLOCK)  ON [d].[MeetingID] = [Meeting].[MeetingID]
INNER JOIN [Course] ON [Meeting].[CourseID] = [Course].[CourseID]
OUTER APPLY (SELECT * FROM [dbo].[GetLeaguePrizes](d.[LeagueID])) t
WHERE (
        ([LeagueSizeID] = 3 AND [Position_Rownum] <= 50)
        OR (d.[UserID] = @UserID AND [LeagueSizeID] = 3)
      )
      OR
      (
        [LeagueSizeID] in (1,2)
      )

ORDER BY [LeagueID], [Position]  

任何方向都会受到赞赏。

1 个答案:

答案 0 :(得分:3)

你需要使用OUTER APPLY(CROSS APPLY和LEFT JOIN的混合)。

SELECT * FROM [League]
INNER JOIN [League_Entry] ON [League].[LeagueID] = [League_Entry].[LeagueID]
OUTER APPLY [dbo].[GetPrizesForLeague]([League].[LeagueID]) t 

CROSS APPLY / OUTER APPLY表现非常好。它非常适合替换一些内部查询和游标。