masanos note

Make Ranking with MySQL

2022-11-25
icon

Version

select version();
+-----------+
| version() |
+-----------+
| 8.0.28    |
+-----------+

Function

ORDER BY

ROW_NUMBER() OVER (ORDER BY column DESC)  AS `ROW ASC`
RANK()       OVER (ORDER BY column DESC)  AS `RANK ASC`
DENSE_RANK() OVER (ORDER BY column DESC)  AS `DENSE ASC`

PARTITION BY

ROW_NUMBER() OVER (PARTITION BY column1 ORDER BY column2 DESC)  AS `ROW ASC`
RANK()       OVER (PARTITION BY column1 ORDER BY column DESC)  AS `RANK ASC`
DENSE_RANK() OVER (PARTITION BY column1 ORDER BY column DESC)  AS `DENSE ASC`

Target Data

When you want to try a function, it's easier to create appropriate data with a with clause and try it.

SELECT 'fruit' 		AS category, 100 AS price, 'banana' 	AS food 
UNION ALL	SELECT 'fruit' 		AS category, 110 AS price, 'apple'  	AS food
UNION ALL	SELECT 'fruit' 		AS category, 120 AS price, 'lemon'  	AS food
UNION ALL	SELECT 'fruit' 		AS category, 120 AS price, 'apple'  	AS food
UNION ALL	SELECT 'fruit' 		AS category, 130 AS price, 'kiwi'   	AS food
UNION ALL	SELECT 'vegetable'  AS category, 100 AS price, 'cabbage' 	AS food 
UNION ALL	SELECT 'vegetable'  AS category, 110 AS price, 'cucumber'  	AS food
UNION ALL	SELECT 'vegetable'  AS category, 120 AS price, 'potato'  	AS food
UNION ALL	SELECT 'vegetable'  AS category, 120 AS price, 'onion'		AS food
UNION ALL	SELECT 'vegetable'  AS category, 130 AS price, 'tomato'   	AS food

Data

+-----------+-------+----------+
| category  | price | food     |
+-----------+-------+----------+
| fruit     |   100 | banana   |
| fruit     |   110 | apple    |
| fruit     |   120 | lemon    |
| fruit     |   120 | apple    |
| fruit     |   130 | kiwi     |
| vegetable |   100 | cabbage  |
| vegetable |   110 | cucumber |
| vegetable |   120 | potato   |
| vegetable |   120 | onion    |
| vegetable |   130 | tomato   |
+-----------+-------+----------+

ROW_NUMBER() OVER (ORDER BY column ASC), RANK(), DENSE_RANK()

ORDER BY column DESC

WITH items AS (
          SELECT 'fruit'      AS category, 100 AS price, 'banana'   AS food 
UNION ALL SELECT 'fruit'      AS category, 110 AS price, 'apple'    AS food
UNION ALL SELECT 'fruit'      AS category, 120 AS price, 'lemon'    AS food
UNION ALL SELECT 'fruit'      AS category, 120 AS price, 'apple'    AS food
UNION ALL SELECT 'fruit'      AS category, 130 AS price, 'kiwi'     AS food
UNION ALL SELECT 'vegetable'  AS category, 100 AS price, 'cabbage'  AS food 
UNION ALL SELECT 'vegetable'  AS category, 110 AS price, 'cucumber' AS food
UNION ALL SELECT 'vegetable'  AS category, 120 AS price, 'potato'   AS food
UNION ALL SELECT 'vegetable'  AS category, 120 AS price, 'onion'    AS food
UNION ALL SELECT 'vegetable'  AS category, 130 AS price, 'tomato'   AS food
)
SELECT
  category
  , food
  , price
  , ROW_NUMBER() OVER (ORDER BY price ASC)  AS `ROW ASC`
  , RANK()       OVER (ORDER BY price ASC)  AS `RANK ASC`
  , DENSE_RANK() OVER (ORDER BY price ASC)  AS `DENSE ASC`
FROM 
  items

Result

+-----------+----------+-------+---------+----------+-----------+
| category  | food     | price | ROW ASC | RANK ASC | DENSE ASC |
+-----------+----------+-------+---------+----------+-----------+
| fruit     | banana   |   100 |       1 |        1 |         1 |
| vegetable | cabbage  |   100 |       2 |        1 |         1 |
| fruit     | apple    |   110 |       3 |        3 |         2 |
| vegetable | cucumber |   110 |       4 |        3 |         2 |
| fruit     | lemon    |   120 |       5 |        5 |         3 |
| fruit     | apple    |   120 |       6 |        5 |         3 |
| vegetable | potato   |   120 |       7 |        5 |         3 |
| vegetable | onion    |   120 |       8 |        5 |         3 |
| fruit     | kiwi     |   130 |       9 |        9 |         4 |
| vegetable | tomato   |   130 |      10 |        9 |         4 |
+-----------+----------+-------+---------+----------+-----------+

ORDER BY column DESC

  , ROW_NUMBER() OVER (ORDER BY price DESC)  AS `ROW DESC`
  , RANK()       OVER (ORDER BY price DESC)  AS `RANK DESC`
  , DENSE_RANK() OVER (ORDER BY price DESC)  AS `DENSE DESC`

Result

+-----------+----------+-------+----------+-----------+------------+
| category  | food     | price | ROW DESC | RANK DESC | DENSE DESC |
+-----------+----------+-------+----------+-----------+------------+
| fruit     | kiwi     |   130 |        1 |         1 |          1 |
| vegetable | tomato   |   130 |        2 |         1 |          1 |
| fruit     | lemon    |   120 |        3 |         3 |          2 |
| fruit     | apple    |   120 |        4 |         3 |          2 |
| vegetable | potato   |   120 |        5 |         3 |          2 |
| vegetable | onion    |   120 |        6 |         3 |          2 |
| fruit     | apple    |   110 |        7 |         7 |          3 |
| vegetable | cucumber |   110 |        8 |         7 |          3 |
| fruit     | banana   |   100 |        9 |         9 |          4 |
| vegetable | cabbage  |   100 |       10 |         9 |          4 |
+-----------+----------+-------+----------+-----------+------------+

PARTITION BY column1 ORDER BY column2 DESC

use PARTITION BY when you want to create rankings by category.

ROW_NUMBER() OVER (PARTITION BY category ORDER BY price ASCAS `ROW ASC`
RANK()       OVER (PARTITION BY category ORDER BY price ASCAS `RANK ASC`
DENSE_RANK() OVER (PARTITION BY category ORDER BY price ASCAS `DENSE ASC`

Result

+-----------+----------+-------+---------+----------+-----------+
| category  | food     | price | ROW ASC | RANK ASC | DENSE ASC |
+-----------+----------+-------+---------+----------+-----------+
| fruit     | banana   |   100 |       1 |        1 |         1 |
| fruit     | apple    |   110 |       2 |        2 |         2 |
| fruit     | lemon    |   120 |       3 |        3 |         3 |
| fruit     | apple    |   120 |       4 |        3 |         3 |
| fruit     | kiwi     |   130 |       5 |        5 |         4 |
| vegetable | cabbage  |   100 |       1 |        1 |         1 |
| vegetable | cucumber |   110 |       2 |        2 |         2 |
| vegetable | potato   |   120 |       3 |        3 |         3 |
| vegetable | onion    |   120 |       4 |        3 |         3 |
| vegetable | tomato   |   130 |       5 |        5 |         4 |
+-----------+----------+-------+---------+----------+-----------+

Supplement

e.g. 1

SELECT
  category
  , food
  , price
  , (
    SELECT
      COUNT(i2.price)
    FROM 
      items AS i2
    WHERE 
      i1.price < i2.price
    ) + 1 AS `rank`
FROM 
  items AS i1
ORDER BY 
  `rank`

Result

+-----------+----------+-------+------+
| category  | food     | price | rank |
+-----------+----------+-------+------+
| fruit     | kiwi     |   130 |    1 |
| vegetable | tomato   |   130 |    1 |
| fruit     | lemon    |   120 |    3 |
| fruit     | apple    |   120 |    3 |
| vegetable | potato   |   120 |    3 |
| vegetable | onion    |   120 |    3 |
| fruit     | apple    |   110 |    7 |
| vegetable | cucumber |   110 |    7 |
| fruit     | banana   |   100 |    9 |
| vegetable | cabbage  |   100 |    9 |
Related Notessupabase with Vue3 Vue3 with bootstrap-icons[vue3]Install bootstrap5[vue3] install[Nuxt3]The first thing I do when launching a nuxt3 project. (^3.5.2)[Nuxt3][Bootstrap]Use Bootstrap icons with Nuxt3.[Node.js] Storing API results in js🔧[GA4][GTM]Configure GA4 in GTM🔧[Nuxt3]Using Google Tag manager with NUXT3🔧[GA4][BigQuery]Linking GA4 and BigQuery🔧When you want to scrape a SPA site, PhantomJsCloud is solution.🐛Error brew -v | update-reset🔧Use Google Spreadsheet as API with Nuxt3.🔧 Get json from a spreadsheet using GoogleSheetsAPI v4.🔧[Python]Install Python to Mac[Nuxt3]Install stable version of Nuxt 3.0.0. | npx nuxi init nuxt3-appwatch & v-model | Vue3 (Nuxt3)window & document | Nuxt3Using custom domain, Hosting to GitHub Pages with Nuxt3GA4 with Nuxt3📝MySQL - Date Function - Tips Use Nuxt3 props🔧Use MicroCMS with Nuxt3🔧Using GoogleFont with Nuxt3📝Error - Deprecation Warning: $weight: Passing a number without unit % (100) is deprecated. - Bootstrap5 📝using sass with nuxt📝Firebase9 Google Authentication with Nuxt3.📝Set favicon in Nuxt3📝Use bootstrap5 with Nuxt3🐛Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement[Nuxt3] How to separate source directoriesmicroCMS & GitHub Actions & Nuxt3Using highlight in Nuxt3.use package.json value🔧frontmatter-markdown-loader & highlight.js🔧Install Font Awesome on Nuxt2 via npm.Github pages with GitHub ActionsCannot find module '~/*/*.vue' or its corresponding type declarations.Vetur(2307)🐛Cannot find module. Consider using '--resolveJsonModule' to import module with '.json' extension.ts(2732)TypeScript Object.🔧Bootstrap5 with Nuxt2processmd with Nuxt2🔧[MySQL]Install MySQL Workbench🔧Convert Markdown to HTML. convert frontmatter to json🔧Install homebrew, nvm, node to Mac🔧[MySQL]Record of installing and starting mysql with homebrew.🎨 Display the photo full screen and overlay the header and footer on top.🔧Set git repository to created project.[Nuxt3] Make Header & Footer
A record of the development is left in a web note.
Masanos
I want to make the world I see happy. Little by little, I am preparing to start a business. Thank you for your support.
Buy Me A Coffee
Copyright© masanos All Rights Reserved.