T-SQL: How To Use DENSE_RANK()
In a previous lesson, we learned that we can apply ROW_NUMBER() to a data set and output the row number of the data set based on the order we specify. This means that if we have an alphabetized list, we could have ROW_NUMBER() return that list in an ascending or descending manner based on the alphabet. What may be a drawback here is that we have possible duplicates in the table, or we have possible multiples in the table and we want these multiples to be the same value rather than being different row numbers. Think of an example where we have millions of home sales organized by state and we want the states ranked by a number, rather than having the rows returned with the Nth value. In the video, SQL Basics: How To Use DENSE_RANK() and Why, we look at using DENSE_RANK() to solve a similar problem to this.
Some questions that are answered in the video:
- You'll notice that the table being used has merchants by names and product ids associated by the merchant. As a note, we would normally use an associative table here, but for the sake of this video, we are using this contrived example, as it helps to illustrate how we can use DENSE_RANK().
- In the video, both ROW_NUMBER() and DENSE_RANK() are compared side-by-side. Using merchant AAA as an example, how do these two return different values?
- Suppose we had 100 home sales by three states of Oklahoma, Kentucky and North Carolina - what would be the maximum DENSE_RANK() value if we ranked by state? What would be the maximum ROW_NUMBER()?
- In considering "groups" and "ordering" what would we use for "ordering" and what would we use for "grouping" when comparing ROW_NUMBER() or DENSE_RANK()?
For mastering data transformation from one form to another form, check out the highest-rated Automating ETL course on Udemy. For a coupon to the course, check out the trailer video on the channel SQL In Six Minutes.
Ordering and grouping data sets help us solve numerous challenges with data. As previously mentioned, a business application of this might be home sales by state (or another area type that we want to group by). Often before we have our "final data product" we'll use this type of functionality to create our data sets and tables. We rarely will have a final data product that looks like the table in the video example: usually we'll have an associative table between these and that associative table can be created from the DENSE_RANK() functionality (provided the grouping remains the same).
Are you looking for tech consultants that can assist with design and development? From building custom applications to working with existing infrastructure that's causing you trouble, we can get you connected to consultants who can assist. You can contact for assistance.