0

How would I go about adding the PERSIST function to a ranking?

I plan on creating a view from this code and will be frequently running subqueries on it. So of course I don't want the ranking number to change based off of whatever subquery I run. I want it to be a normal, non-formula-like column.

    SELECT  a.player_id, a.full_name, a.first_name, a.last_name, d.[rank], 
    d.column3, a.tournament, c.column5, a.[year], a.[round], 
    a.score, RANK() OVER (PARTITION BY a.[round] + b.tournament_id + a.
    [year] ORDER BY a.score) AS round_leaderboard
    FROM            df1 AS a JOIN
    df2 AS b ON a.tournament = b.tournament JOIN
    df3 AS c ON c.[Event] = a.tournament
    LEFT JOIN df4 as d
    On a.player_id=d.player_id and a.[year]=d.[year]

1 Answers1

2

You can use a materialized view:

create view myview with schemabinding as
    SELECT  a.player_id, a.full_name, a.first_name, a.last_name, d.[rank], 
            d.column3, a.tournament, c.column5, a.[year], a.[round], 
            a.score,
            RANK() OVER (PARTITION BY a.[round] + b.tournament_id + a.[year] ORDER BY a.score) AS round_leaderboard
    FROM df1 a JOIN
         df2 b
         ON a.tournament = b.tournament JOIN
         df3 c
         ON c.[Event] = a.tournament LEFT JOIN
         df4 d
         On a.player_id = d.player_id and a.[year] = d.[year];

create index idx_myview_playerid on myview(playerid);

Materialized views are explained in the documentation.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786