Applying RANK using Windowing Functions

#1

Let us see how we can get sparse ranks using 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 sparse ranks using daily_product_revenue with in each day based on revenue.

USE training_retail;

SELECT t.*,
  rank() OVER (
    PARTITION BY order_date
    ORDER BY revenue DESC
  ) AS rnk
FROM daily_product_revenue t
ORDER BY order_date, revenue DESC
LIMIT 100;

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

USE training_hr;

SELECT
  employee_id,
  department_id,
  salary,
  rank() OVER (
    PARTITION BY department_id
    ORDER BY salary DESC
  ) rnk
FROM employees
ORDER BY department_id, salary DESC;

Practice hive on state of the art Big Data cluster - https://labs.itversity.com
You can sign up for our courses on Udemy using $10 coupons - Udemy Coupons - Big Data Courses


0 Likes