Advertisement
hadimaster65555

SQL - Introduction to Window Function - Rank Function Script by HadiMaster

Apr 4th, 2024
1,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PostgreSQL 3.22 KB | Source Code | 0 0
  1. -- Create the table
  2. CREATE TABLE sales_data (
  3.     ProductID SERIAL PRIMARY KEY,
  4.     ProductName VARCHAR(50),
  5.     Price DECIMAL(10, 2),
  6.     ProductGroup VARCHAR(20)
  7. );
  8.  
  9. -- Insert data into the table
  10. INSERT INTO sales_data (ProductName, Price, ProductGroup) VALUES
  11. ('Product A', 10.99, 'Electronics'),
  12. ('Product B', 15.49, 'Clothing'),
  13. ('Product C', 20.99, 'Electronics'),
  14. ('Product D', 12.99, 'Home'),
  15. ('Product E', 8.99, 'Clothing'),
  16. ('Product F', 30.99, 'Electronics'),
  17. ('Product G', 25.49, 'Home'),
  18. ('Product H', 18.99, 'Clothing'),
  19. ('Product I', 22.99, 'Electronics'),
  20. ('Product J', 17.99, 'Home'),
  21. ('Product K', 9.99, 'Clothing'),
  22. ('Product L', 14.99, 'Electronics'),
  23. ('Product M', 11.49, 'Home'),
  24. ('Product N', 27.99, 'Electronics'),
  25. ('Product O', 21.99, 'Clothing'),
  26. ('Product P', 16.99, 'Home'),
  27. ('Product Q', 19.99, 'Electronics'),
  28. ('Product R', 13.99, 'Clothing'),
  29. ('Product S', 28.49, 'Home'),
  30. ('Product T', 23.99, 'Electronics'),
  31. ('Product U', 10.49, 'Clothing'),
  32. ('Product V', 26.99, 'Home'),
  33. ('Product W', 18.49, 'Electronics'),
  34. ('Product X', 14.49, 'Clothing'),
  35. ('Product Y', 29.99, 'Home'),
  36. ('Product Z', 24.49, 'Electronics'),
  37. ('Product AA', 11.99, 'Clothing'),
  38. ('Product AB', 15.99, 'Home'),
  39. ('Product AC', 20.49, 'Electronics'),
  40. ('Product AD', 17.49, 'Clothing'),
  41. ('Product BB', 17.49, 'Clothing');
  42.  
  43. -- see all data
  44. SELECT * FROM sales_data;
  45.  
  46. -- ROW_NUMBER() Syntax
  47.  
  48. -- put row number based on price that sorted descendingly
  49. SELECT
  50.     ProductID,
  51.     ProductName,
  52.     Price,
  53.     ProductGroup,
  54.     ROW_NUMBER() OVER (ORDER BY Price DESC) AS row_num
  55. FROM
  56.     sales_data;
  57.    
  58. -- put row number based on table that
  59. -- partitioned by product group and price that sorted ascendingly
  60. SELECT
  61.     ProductID,
  62.     ProductName,
  63.     Price,
  64.     ProductGroup,
  65.     ROW_NUMBER() OVER (PARTITION BY ProductGroup ORDER BY Price) AS group_row_num
  66. FROM
  67.     sales_data;
  68.  
  69. -- RANK
  70.  
  71. -- put rank based on table that
  72. -- partitioned by product group and price that sorted ascendingly
  73. SELECT
  74.     ProductID,
  75.     ProductName,
  76.     Price,
  77.     ProductGroup,
  78.     RANK() OVER (PARTITION BY ProductGroup ORDER BY Price DESC) AS GroupRank
  79. FROM
  80.     sales_data;
  81.  
  82. -- put rank based on price that sorted descendingly
  83. SELECT
  84.     ProductID,
  85.     ProductName,
  86.     Price,
  87.     ProductGroup,
  88.     RANK() OVER (ORDER BY Price DESC) AS OverallRank
  89. FROM
  90.     sales_data;
  91.  
  92. -- DENSE_RANK() Syntax
  93.  
  94. -- put dense rank based on price and product name that sorted descendingly
  95. -- and partitioned by product group
  96. SELECT
  97.     ProductID,
  98.     ProductName,
  99.     Price,
  100.     ProductGroup,
  101.     DENSE_RANK() OVER (PARTITION BY ProductGroup ORDER BY Price, ProductName) AS PriceNameRank
  102. FROM
  103.     sales_data;
  104.  
  105.  
  106. -- NTILE() Syntax
  107.  
  108. -- devide data in 4 groups (quartile) based on price that sorted ascendingly
  109. SELECT
  110.     ProductID,
  111.     ProductName,
  112.     Price,
  113.     ProductGroup,
  114.     NTILE(4) OVER (ORDER BY Price) AS Quartile
  115. FROM
  116.     sales_data;
  117.    
  118. -- devide data in 4 groups (quartile) based on price that sorted ascendingly
  119. -- and partitioned by product group
  120. SELECT
  121.     ProductID,
  122.     ProductName,
  123.     Price,
  124.     ProductGroup,
  125.     NTILE(4) OVER (PARTITION BY ProductGroup ORDER BY Price) AS GroupRank
  126. FROM
  127.     sales_data;
  128.  
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement