I can teach you analytics! I have never seen a database do analytics better than Snowflake. This week we are working on the RANK command.
In each example, you will see an ORDER BY statement, but it will not come at the end of the query. The ORDER BY keywords is always within the RANK calculation. It is the ORDER BY statement that determines what we are ranking.
In its most simple explanation, a RANK will sort the data first via the ORDER BY statement, and then give the first row rank of 1. It will rank the second row with a two unless the value of row one and row two are equal. Check out the example below.
In the picture above, notice that an open and close parenthesis immediately follows the keyword RANK. The open and close parenthesis insinuates a function. There is never anything inside the parenthesis, but it is required. The keyword OVER follows, which represents that this analytic is an ordered analytic, which is interchangeable with the term window function. The term ordered analytic means that the data set will be put in a specific order before the calculation begins. We sort the data using the ORDER BY statement, and in this example, we are sorting by the Daily_Sales column. Since the default for an ORDER BY statement is ascending, we are ranking the data by Daily_Sales ASC.
When you first see a RANK command with nothing in the parenthesis, you might have trouble at first and think, “What are we ranking?” Just check out the column in the ORDER BY statement, and that is what you are ranking. We have an ORDER BY Daily_Sales, so we are ranking by Daily_Sales.
The first two rows both have a value of 32,800.50, so they both get a rank of 1. Rows one and two are tied, but notice that row three gets a rank of 3. There are no more ties after that so each row gets the next sequential ranking.
Quite often, a user will want to give the highest value the number one rank. Check out the example below because we are going to use the ORDER BY to rank the Daily_Sales column in DESC order.
The phrase below sounds like a protest chant:
What are we ranking? Daily_Sales!
How are we ranking it? Descending order!
Each RANK example will have an ORDER BY statement, but sometimes you will also have a PARTITION statement. In the example below, you see the keywords PARTITION BY, and that means the RANK function will reset and start over. Our ORDER BY statement is ordering the data by the column Daily_Sales DESC, so Daily_Sales is what we are ranking, but we will reset the rank calculation and start over with each Product_ID break because the column Product_ID is in the PARTITION BY statement. Check out the next example below.
Now, get ready for something you have never seen before, which is the QUALIFY statement. QUALIFY acts like a filter, but it is different than a WHERE clause filter. The QUALIFY statement waits until all of the analytic calculations finish. When the report finishes and is ready to return, the QUALIFY steps in and filters the rows further.
In the example below, we are attempting to find the top three Daily_Sales per Product_ID. It is the QUALIFY statement that dictates the top three Daily_Sales.
Let’s review. We rank by Daily_Sales DESC because of the ORDER BY Daily_Sales DESC statement. We reset the calculation on each Product_ID break because of the PARTITION BY Product_ID statement. And finally, after the entire ranking occurs, we further filter to get only the top three Daily_Sales per Product_ID because of the Qualify statement. Notice we have given our RANK analytic an alias name for the report in which we call RANK1. So, we QUALIFY RANK1 < 4, which means give me the first three rows.
The only two systems I have ever seen that use the QUALIFY statements are Snowflake and Teradata. If you are working with a system that does not support QUALIFY, you can place your SQL statement in a derived table, and then use a WHERE clause.
In the example below, all of the SQL using the color blue and red is part of the derived table. The results are the same as the previous example, but it is done with a derived table and not a QUALIFY statement.
You are about to see something not often seen, and that is the DENSE_RANK. It works exactly like the RANK command, but RANK and DENSE_RANK handle ties differently. In our example below, we are using both the RANK and DENSE_RANK functions. Since both of them have the same ORDER BY Daily_Sales statement, the data comes out the same with the only difference being how they handle the ties. The first two rows get a rank of one since they both tie with a value of 32,800.50. The RANK gives the third row a ranking of three, and the DENSE_RANK gives it the third-row a ranking of two.
The example below shows the PERCENT_RANK function. Percent_Rank finds out the relative rank of a row in a group. The formula to get Percent_Rank is
(RANK-1 / (Total Rows -1).
If you want to move data to snowflake or you want to use the greatest query tool known to humankind, then use the Nexus. Download your free Nexus trial at www.CoffingDW.com.
Nexus has the advanced ability to take an answer set and then provide additional analytic reports through point-and-click templates. Nexus provides over 80 different analytics with a few clicks of the mouse. Check out the next two screenshots and watch how easy it is to get a report with Rank, Dense_Rank, and Percent_Rank using Nexus.
Then press on the RANK tab. Tell Nexus what column you want to rank, and the column to partition. Place a checkmark on the Rank/P, Dense_Rank/P, and Percent_Rank/p, and then hit CREATE. Yes, it is that easy and powerful. Nexus performs all calculations inside your PC.
I hope you enjoyed today’s Snowflake analytic lesson. See you next week.
CEO, Coffing Data Warehousing
Direct: 513 300-0341
Youtube channel: CoffingDW