Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Ordering Rows in a Window3:02 with Danielle Kiowski
Grouping Data With Partitions
ORDER BY — Keyword to introduce the ordering clause. Followed by expressions that determine the order of rows within the window.
These functions can only be used as window functions because they rely on the context of other rows.
- RANK() — Ranks each row based on the window function’s ORDER BY clause
- FIRST_VALUE(expression) — Returns the value of the expression for the first record in the frame/partition
- LAST_VALUE(expression) — Returns the value of the expression for the last record in the frame/partition
- LEAD(expression,n,default) — Returns the value of the expression for the row n rows after the current row in the partition. If the specified row does not exist, returns the default value. If n is not specified, defaults to 1 (the next row)
- LAG(expression,n,default) — Returns the value of the expression for the row n rows before the current row in the partition. If the specified row does not exist, returns the default value. If n is not specified, defaults to 1 (the previous row)
See more in the PostgreSQL documentation: https://www.postgresql.org/docs/devel/static/functions-window.html
SELECT PLAYER_NAME, PLAYER_RANKING, PLAYER_TEAM, COUNT(MATCH_ID) as MATCHES_PLAYED, SUM(CHECKMATE) as MATCHES_WON, SUM(CHECKMATE)/COUNT(MATCH_ID) as WIN_RATE, AVG(PLAYER_RANKING) OVER () as AVG_RANKING, PLAYER_RANKING - AVG(PLAYER_RANKING) OVER () as DIFF_FROM_AVERAGE, SUM(SUM(CHECKMATE)) OVER () as TOTAL_MATCHES, SUM(MAX(CHECKMATE)) OVER () as PLAYERS_WITH_WINS, MAX(SUM(CHECKMATE)) OVER () as HIGHEST_WINS, SUM(SUM(CHECKMATE)) OVER (PARTITION BY PLAYER_TEAM) /SUM(COUNT(MATCH_ID)) OVER (PARTITION BY PLAYER_TEAM) as TEAM_WIN_RATE, MIN(PLAYER_RANKING) OVER (PARTITION BY SUM(CHECKMATE)) as TIEBREAKER, MIN(PLAYER_RANKING) OVER (PARTITION BY SUM(CHECKMATE), PLAYER_TEAM) as TIEBREAKER_TEAM, RANK() OVER (PARTITION BY PLAYER_TEAM ORDER BY PLAYER_RANKING DESC) as RANKING FROM treehouse.chess_data_matches GROUP BY PLAYER_NAME, PLAYER_RANKING,PLAYER_TEAM
Some functions are only available as window functions,
not as regular aggregates.
This is because they are only useful in the context of other records but not for
the row itself.
To answer our fourth question, who is the top ranked player on each team?
We'll use one of these functions.
Rank, it gives the ranking of each row within the window, starting with one.
Windows specific functions order the rows within the partition
to determine whether one row comes before or after another.
The order clause,
which is specified within the parentheses after the partition,
gives us the ability to define the ordering of rows within the window.
Let's use rank to find the top ranked players in the tournament.
We'll add a new field to the report using this function, and
then add the over keyword.
This won't run yet
because the function has no way of determining how to rank the players.
We might want to rank them alphabetically by name or by number of matches one.
To answer the question, we want to order them by their ranking, so
we need to specify that in the order for the rank function.
We'll introduce the clause inside the parentheses with the words, order by,
followed by the expression used to determine order, player ranking.
Like with partitions,
you can pass multiple expressions to order by clauses and you can order by
aggregates as long as they return one value per row of the result set.
When we this ranks the players, but it's the opposite of what we want.
The lowest ranked players ranked first and
the highest ranked players have high rankings.
We can easily reverse the order by specifying a sort order for
The default is ASC or ascending.
Since we want the rank to start with the highest ranking, we'll specify DESC for
descending This gives us the ranking for each player across the entire tournament.
We want to know the rank within the team, so
we'll add in a partition by player team so that the ranking is done within each team.
The partition clause goes inside the parenthesis before the order by clause.
When we run that, we get the ranking for each player within their team.
It's a little bit hard to see right now because the records are still ordered
by number of wins and that doesn't match the order by player ranking exactly.
Let's change that so
that it's easier to see which players are at the top of their team.
To avoid typing the entire window function again,
we use the short cut where we use the number of the field.
In this case, our rank window function is the 15th field.
Right now we have DESC specified, but we want to change that to AFC so
that the number one ranked player will be at the top.
Let's run that.
Now we can see that Xavier, Kate, and
Gina are the highest ranked players on their respective teams.
There are other window functions which provide information about the rows that
appear before or after a particular row, or determine the first or
last value in a window.
Check the teacher's notes for more information on those.
You need to sign up for Treehouse in order to download course files.Sign up