Applying DENSE_RANK using Windowing Functions


Let us see how we can get dense ranks using dense_rank function.

  • If we have to get ranks globally, we just need to specify ORDER BY
  • If we have to get ranks with in a key then we need to specify PARTITION BY and then ORDER BY.
  • By default ORDER BY will sort the data in ascending order. We can change the order by passing DESC after order by.

Here is an example to assign dense ranks using daily_product_revenue with in each day based on revenue.

USE training_retail;

  dense_rank() OVER (
    PARTITION BY order_date
    ORDER BY revenue DESC
  ) AS drnk
FROM daily_product_revenue t
ORDER BY order_date, revenue DESC
LIMIT 100;

Here is another example to assign dense ranks using employees data set with in each department.

USE training_hr;

  dense_rank() OVER (
    PARTITION BY department_id
    ORDER BY salary DESC
  ) drnk
FROM employees
ORDER BY department_id, salary DESC;

Practice hive on state of the art Big Data cluster -
You can sign up for our courses on Udemy using $10 coupons - Udemy Coupons - Big Data Courses