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.
SQL In Six Minutes | Automating ETL | T-SQL In 2 Hours | Consumer Guide To Digital Security
Leave T-SQL: How To Use DENSE_RANK() to:
Read more #development posts
Best Posts From SqlinSix
We have not curated any of sqlinsix's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From SqlinSix
- Solution When ISJSON May Not Be A Recognized Function Name
- Creating a GUID For Each GUID
- Get A Past Bash Command Quickly
- Connecting A Data Factory To An Existing Runtime In Azure
- Backing Up A Database With MongoDump
- SQL Tutorial: Using UNION ALL or UNION and Why
- T-SQL: How To UNION ALL Tables and Why
- SQL Tutorial: Solving Data Differentials With LEFT JOINs Only
- Solving Data Differentials With LEFT JOINs
- Spark Scala: Grouping Values In A Key-Value Pair
- Transaction Log Becoming Full Due To Replication
- Why We Should Be Skeptical About Data Regarding the Physical World
- Does the Same Data Mean the Same Conclusions?
- SQL Tutorial: How To Use ROW NUMBER() and Why
- Not Able To Drop Table Because of Reference
- T-SQL: How To Use DENSE_RANK()
- The Decentralized Myth - Beyond the Hype
- SQL Tutorial: INNER JOINs vs LEFT JOINs
- T-SQL: How To Use PARTITION BY and Why
- Weekly Tech Lesson: CROSS JOINs Versus CROSS APPLY