{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Analytic Functions – Ranking\n", "\n", "Let us see how we can assign ranks using different **rank** functions." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "tags": [ "remove-cell" ] }, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%HTML\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* If we have to assign ranks globally, we just need to specify **ORDER BY**\n", "* If we have to assign ranks with in a key then we need to specify **PARTITION BY** and then **ORDER BY**.\n", "* By default **ORDER BY** will sort the data in ascending order. We can change the order by passing **DESC** after order by.\n", "* We have 3 main functions to assign ranks - `rank`, `dense_rank` and `row_number`. We will see the differences between the 3 in a moment." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The sql extension is already loaded. To reload it, use:\n", " %reload_ext sql\n" ] } ], "source": [ "%load_ext sql" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "env: DATABASE_URL=postgresql://itversity_retail_user:retail_password@localhost:5432/itversity_retail_db\n" ] } ], "source": [ "%env DATABASE_URL=postgresql://itversity_retail_user:retail_password@localhost:5432/itversity_retail_db" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "Here is an example to assign sparse ranks using daily_product_revenue with in each day based on revenue.\n", "```" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db\n", "30 rows affected.\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
order_dateorder_item_product_idrevenuernk
2013-07-25 00:00:0010045599.721
2013-07-25 00:00:001915099.492
2013-07-25 00:00:009574499.703
2013-07-25 00:00:003653359.444
2013-07-25 00:00:0010732999.855
2013-07-25 00:00:0010142798.886
2013-07-25 00:00:004031949.857
2013-07-25 00:00:005021650.008
2013-07-25 00:00:006271079.739
2013-07-25 00:00:00226599.9910
2013-07-25 00:00:0024319.9611
2013-07-25 00:00:00821207.9612
2013-07-25 00:00:00625199.9913
2013-07-25 00:00:00705119.9914
2013-07-25 00:00:00572119.9715
2013-07-25 00:00:00666109.9916
2013-07-25 00:00:00725108.0017
2013-07-25 00:00:00134100.0018
2013-07-25 00:00:0090699.9619
2013-07-25 00:00:0082895.9720
2013-07-25 00:00:0081079.9621
2013-07-25 00:00:0092479.9522
2013-07-25 00:00:0092679.9522
2013-07-25 00:00:009374.9724
2013-07-25 00:00:0083563.9825
2013-07-25 00:00:0089749.9826
2013-07-26 00:00:00100410799.461
2013-07-26 00:00:003657978.672
2013-07-26 00:00:009576899.543
2013-07-26 00:00:001916799.324
" ], "text/plain": [ "[(datetime.datetime(2013, 7, 25, 0, 0), 1004, Decimal('5599.72'), 1),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 191, Decimal('5099.49'), 2),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 957, Decimal('4499.70'), 3),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 365, Decimal('3359.44'), 4),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 1073, Decimal('2999.85'), 5),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 1014, Decimal('2798.88'), 6),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 403, Decimal('1949.85'), 7),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 502, Decimal('1650.00'), 8),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 627, Decimal('1079.73'), 9),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 226, Decimal('599.99'), 10),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 24, Decimal('319.96'), 11),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 821, Decimal('207.96'), 12),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 625, Decimal('199.99'), 13),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 705, Decimal('119.99'), 14),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 572, Decimal('119.97'), 15),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 666, Decimal('109.99'), 16),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 725, Decimal('108.00'), 17),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 134, Decimal('100.00'), 18),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 906, Decimal('99.96'), 19),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 828, Decimal('95.97'), 20),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 810, Decimal('79.96'), 21),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 924, Decimal('79.95'), 22),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 926, Decimal('79.95'), 22),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 93, Decimal('74.97'), 24),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 835, Decimal('63.98'), 25),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 897, Decimal('49.98'), 26),\n", " (datetime.datetime(2013, 7, 26, 0, 0), 1004, Decimal('10799.46'), 1),\n", " (datetime.datetime(2013, 7, 26, 0, 0), 365, Decimal('7978.67'), 2),\n", " (datetime.datetime(2013, 7, 26, 0, 0), 957, Decimal('6899.54'), 3),\n", " (datetime.datetime(2013, 7, 26, 0, 0), 191, Decimal('6799.32'), 4)]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%sql\n", "\n", "SELECT t.*,\n", " rank() OVER (\n", " PARTITION BY order_date\n", " ORDER BY revenue DESC\n", " ) AS rnk\n", "FROM daily_product_revenue t\n", "ORDER BY order_date, revenue DESC\n", "LIMIT 30" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "Here is another example to assign sparse ranks using employees data set with in each department. Make sure to restart kernel as you might have connected to retail database.\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext sql" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "env: DATABASE_URL=postgresql://itversity_hr_user:hr_password@localhost:5432/itversity_hr_db\n" ] } ], "source": [ "%env DATABASE_URL=postgresql://itversity_hr_user:hr_password@localhost:5432/itversity_hr_db" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 rows affected.\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
employee_iddepartment_idsalary
200104400.00
2012013000.00
202206000.00
1143011000.00
115303100.00
116302900.00
117302800.00
118302600.00
119302500.00
203406500.00
" ], "text/plain": [ "[(200, 10, Decimal('4400.00')),\n", " (201, 20, Decimal('13000.00')),\n", " (202, 20, Decimal('6000.00')),\n", " (114, 30, Decimal('11000.00')),\n", " (115, 30, Decimal('3100.00')),\n", " (116, 30, Decimal('2900.00')),\n", " (117, 30, Decimal('2800.00')),\n", " (118, 30, Decimal('2600.00')),\n", " (119, 30, Decimal('2500.00')),\n", " (203, 40, Decimal('6500.00'))]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%sql\n", "\n", "SELECT employee_id, department_id, salary FROM employees \n", "ORDER BY department_id,\n", " salary DESC\n", "LIMIT 10" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * postgresql://itversity_hr_user:***@localhost:5432/itversity_hr_db\n", "20 rows affected.\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
employee_iddepartment_idsalaryrnk
200104400.001
2012013000.001
202206000.002
1143011000.001
115303100.002
116302900.003
117302800.004
118302600.005
119302500.006
203406500.001
121508200.001
120508000.002
122507900.003
123506500.004
124505800.005
184504200.006
185504100.007
192504000.008
193503900.009
188503800.0010
" ], "text/plain": [ "[(200, 10, Decimal('4400.00'), 1),\n", " (201, 20, Decimal('13000.00'), 1),\n", " (202, 20, Decimal('6000.00'), 2),\n", " (114, 30, Decimal('11000.00'), 1),\n", " (115, 30, Decimal('3100.00'), 2),\n", " (116, 30, Decimal('2900.00'), 3),\n", " (117, 30, Decimal('2800.00'), 4),\n", " (118, 30, Decimal('2600.00'), 5),\n", " (119, 30, Decimal('2500.00'), 6),\n", " (203, 40, Decimal('6500.00'), 1),\n", " (121, 50, Decimal('8200.00'), 1),\n", " (120, 50, Decimal('8000.00'), 2),\n", " (122, 50, Decimal('7900.00'), 3),\n", " (123, 50, Decimal('6500.00'), 4),\n", " (124, 50, Decimal('5800.00'), 5),\n", " (184, 50, Decimal('4200.00'), 6),\n", " (185, 50, Decimal('4100.00'), 7),\n", " (192, 50, Decimal('4000.00'), 8),\n", " (193, 50, Decimal('3900.00'), 9),\n", " (188, 50, Decimal('3800.00'), 10)]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%sql\n", "\n", "SELECT employee_id, department_id, salary,\n", " rank() OVER (\n", " PARTITION BY department_id \n", " ORDER BY salary DESC\n", " ) AS rnk\n", "FROM employees\n", "LIMIT 20" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "Here is an example to assign dense ranks using employees data set with in each department.\n", "```" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * postgresql://itversity_hr_user:***@localhost:5432/itversity_hr_db\n", "20 rows affected.\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
employee_iddepartment_idsalarydrnk
200104400.001
2012013000.001
202206000.002
1143011000.001
115303100.002
116302900.003
117302800.004
118302600.005
119302500.006
203406500.001
121508200.001
120508000.002
122507900.003
123506500.004
124505800.005
184504200.006
185504100.007
192504000.008
193503900.009
188503800.0010
" ], "text/plain": [ "[(200, 10, Decimal('4400.00'), 1),\n", " (201, 20, Decimal('13000.00'), 1),\n", " (202, 20, Decimal('6000.00'), 2),\n", " (114, 30, Decimal('11000.00'), 1),\n", " (115, 30, Decimal('3100.00'), 2),\n", " (116, 30, Decimal('2900.00'), 3),\n", " (117, 30, Decimal('2800.00'), 4),\n", " (118, 30, Decimal('2600.00'), 5),\n", " (119, 30, Decimal('2500.00'), 6),\n", " (203, 40, Decimal('6500.00'), 1),\n", " (121, 50, Decimal('8200.00'), 1),\n", " (120, 50, Decimal('8000.00'), 2),\n", " (122, 50, Decimal('7900.00'), 3),\n", " (123, 50, Decimal('6500.00'), 4),\n", " (124, 50, Decimal('5800.00'), 5),\n", " (184, 50, Decimal('4200.00'), 6),\n", " (185, 50, Decimal('4100.00'), 7),\n", " (192, 50, Decimal('4000.00'), 8),\n", " (193, 50, Decimal('3900.00'), 9),\n", " (188, 50, Decimal('3800.00'), 10)]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%sql\n", "\n", "SELECT employee_id, department_id, salary,\n", " dense_rank() OVER (\n", " PARTITION BY department_id \n", " ORDER BY salary DESC\n", " ) AS drnk\n", "FROM employees\n", "LIMIT 20" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "Here is an example for global rank based on salary. If all the salaries are unique, we can use `LIMIT` but when they are not unique, we have to go with analytic functions.\n", "```" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * postgresql://itversity_hr_user:***@localhost:5432/itversity_hr_db\n", "20 rows affected.\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
employee_iddepartment_idsalaryrnkdrnk
1009024000.0011
1019017000.0022
1029017000.0022
1458014000.0043
1468013500.0054
2012013000.0065
20511012000.0076
1478012000.0076
10810012000.0076
1688011500.00107
1488011000.00118
1748011000.00118
1143011000.00118
1498010500.00149
1628010500.00149
1698010000.001610
2047010000.001610
1508010000.001610
1568010000.001610
170809600.002011
" ], "text/plain": [ "[(100, 90, Decimal('24000.00'), 1, 1),\n", " (101, 90, Decimal('17000.00'), 2, 2),\n", " (102, 90, Decimal('17000.00'), 2, 2),\n", " (145, 80, Decimal('14000.00'), 4, 3),\n", " (146, 80, Decimal('13500.00'), 5, 4),\n", " (201, 20, Decimal('13000.00'), 6, 5),\n", " (205, 110, Decimal('12000.00'), 7, 6),\n", " (147, 80, Decimal('12000.00'), 7, 6),\n", " (108, 100, Decimal('12000.00'), 7, 6),\n", " (168, 80, Decimal('11500.00'), 10, 7),\n", " (148, 80, Decimal('11000.00'), 11, 8),\n", " (174, 80, Decimal('11000.00'), 11, 8),\n", " (114, 30, Decimal('11000.00'), 11, 8),\n", " (149, 80, Decimal('10500.00'), 14, 9),\n", " (162, 80, Decimal('10500.00'), 14, 9),\n", " (169, 80, Decimal('10000.00'), 16, 10),\n", " (204, 70, Decimal('10000.00'), 16, 10),\n", " (150, 80, Decimal('10000.00'), 16, 10),\n", " (156, 80, Decimal('10000.00'), 16, 10),\n", " (170, 80, Decimal('9600.00'), 20, 11)]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%sql\n", "\n", "SELECT employee_id, department_id, salary,\n", " rank() OVER (\n", " ORDER BY salary DESC\n", " ) AS rnk,\n", " dense_rank() OVER (\n", " ORDER BY salary DESC\n", " ) AS drnk\n", "FROM employees\n", "LIMIT 20" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us understand the difference between **rank**, **dense_rank** and **row_number**.\n", "\n", "* We can use either of the functions to generate ranks when the rank field does not have duplicates.\n", "* When rank field have duplicates then row_number should not be used as it generate unique number for each record with in the partition.\n", "* **rank** will skip the ranks in between if multiple people get the same rank while **dense_rank** continue with the next number." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The sql extension is already loaded. To reload it, use:\n", " %reload_ext sql\n" ] } ], "source": [ "%load_ext sql" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "env: DATABASE_URL=postgresql://itversity_hr_user:hr_password@localhost:5432/itversity_hr_db\n" ] } ], "source": [ "%env DATABASE_URL=postgresql://itversity_hr_user:hr_password@localhost:5432/itversity_hr_db" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * postgresql://itversity_hr_user:***@localhost:5432/itversity_hr_db\n", "50 rows affected.\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
employee_iddepartment_idsalaryrnkdrnkrn
200104400.00111
2012013000.00111
202206000.00222
1143011000.00111
115303100.00222
116302900.00333
117302800.00444
118302600.00555
119302500.00666
203406500.00111
121508200.00111
120508000.00222
122507900.00333
123506500.00444
124505800.00555
184504200.00666
185504100.00777
192504000.00888
193503900.00999
188503800.00101010
137503600.00111111
189503600.00111112
141503500.00131213
186503400.00141314
129503300.00151415
133503300.00151416
125503200.00171517
138503200.00171518
180503200.00171519
194503200.00171520
142503100.00211621
181503100.00211622
196503100.00211623
187503000.00241724
197503000.00241725
134502900.00261826
190502900.00261827
130502800.00281928
183502800.00281929
195502800.00281930
126502700.00312031
139502700.00312032
143502600.00332133
198502600.00332134
199502600.00332135
131502500.00362236
140502500.00362237
144502500.00362238
182502500.00362239
191502500.00362240
" ], "text/plain": [ "[(200, 10, Decimal('4400.00'), 1, 1, 1),\n", " (201, 20, Decimal('13000.00'), 1, 1, 1),\n", " (202, 20, Decimal('6000.00'), 2, 2, 2),\n", " (114, 30, Decimal('11000.00'), 1, 1, 1),\n", " (115, 30, Decimal('3100.00'), 2, 2, 2),\n", " (116, 30, Decimal('2900.00'), 3, 3, 3),\n", " (117, 30, Decimal('2800.00'), 4, 4, 4),\n", " (118, 30, Decimal('2600.00'), 5, 5, 5),\n", " (119, 30, Decimal('2500.00'), 6, 6, 6),\n", " (203, 40, Decimal('6500.00'), 1, 1, 1),\n", " (121, 50, Decimal('8200.00'), 1, 1, 1),\n", " (120, 50, Decimal('8000.00'), 2, 2, 2),\n", " (122, 50, Decimal('7900.00'), 3, 3, 3),\n", " (123, 50, Decimal('6500.00'), 4, 4, 4),\n", " (124, 50, Decimal('5800.00'), 5, 5, 5),\n", " (184, 50, Decimal('4200.00'), 6, 6, 6),\n", " (185, 50, Decimal('4100.00'), 7, 7, 7),\n", " (192, 50, Decimal('4000.00'), 8, 8, 8),\n", " (193, 50, Decimal('3900.00'), 9, 9, 9),\n", " (188, 50, Decimal('3800.00'), 10, 10, 10),\n", " (137, 50, Decimal('3600.00'), 11, 11, 11),\n", " (189, 50, Decimal('3600.00'), 11, 11, 12),\n", " (141, 50, Decimal('3500.00'), 13, 12, 13),\n", " (186, 50, Decimal('3400.00'), 14, 13, 14),\n", " (129, 50, Decimal('3300.00'), 15, 14, 15),\n", " (133, 50, Decimal('3300.00'), 15, 14, 16),\n", " (125, 50, Decimal('3200.00'), 17, 15, 17),\n", " (138, 50, Decimal('3200.00'), 17, 15, 18),\n", " (180, 50, Decimal('3200.00'), 17, 15, 19),\n", " (194, 50, Decimal('3200.00'), 17, 15, 20),\n", " (142, 50, Decimal('3100.00'), 21, 16, 21),\n", " (181, 50, Decimal('3100.00'), 21, 16, 22),\n", " (196, 50, Decimal('3100.00'), 21, 16, 23),\n", " (187, 50, Decimal('3000.00'), 24, 17, 24),\n", " (197, 50, Decimal('3000.00'), 24, 17, 25),\n", " (134, 50, Decimal('2900.00'), 26, 18, 26),\n", " (190, 50, Decimal('2900.00'), 26, 18, 27),\n", " (130, 50, Decimal('2800.00'), 28, 19, 28),\n", " (183, 50, Decimal('2800.00'), 28, 19, 29),\n", " (195, 50, Decimal('2800.00'), 28, 19, 30),\n", " (126, 50, Decimal('2700.00'), 31, 20, 31),\n", " (139, 50, Decimal('2700.00'), 31, 20, 32),\n", " (143, 50, Decimal('2600.00'), 33, 21, 33),\n", " (198, 50, Decimal('2600.00'), 33, 21, 34),\n", " (199, 50, Decimal('2600.00'), 33, 21, 35),\n", " (131, 50, Decimal('2500.00'), 36, 22, 36),\n", " (140, 50, Decimal('2500.00'), 36, 22, 37),\n", " (144, 50, Decimal('2500.00'), 36, 22, 38),\n", " (182, 50, Decimal('2500.00'), 36, 22, 39),\n", " (191, 50, Decimal('2500.00'), 36, 22, 40)]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%sql\n", "\n", "SELECT\n", " employee_id,\n", " department_id,\n", " salary,\n", " rank() OVER (\n", " PARTITION BY department_id\n", " ORDER BY salary DESC\n", " ) rnk,\n", " dense_rank() OVER (\n", " PARTITION BY department_id\n", " ORDER BY salary DESC\n", " ) drnk,\n", " row_number() OVER (\n", " PARTITION BY department_id\n", " ORDER BY salary DESC, employee_id\n", " ) rn\n", "FROM employees\n", "ORDER BY department_id, salary DESC\n", "LIMIT 50" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "Here is another example to with respect to all 3 functions. Make sure to restart kernel as you might have connected to HR database.\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext sql" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "env: DATABASE_URL=postgresql://itversity_retail_user:retail_password@localhost:5432/itversity_retail_db\n" ] } ], "source": [ "%env DATABASE_URL=postgresql://itversity_retail_user:retail_password@localhost:5432/itversity_retail_db" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "30 rows affected.\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
order_dateorder_item_product_idrevenuernkdrnkrn
2013-07-25 00:00:0010045599.72111
2013-07-25 00:00:001915099.49222
2013-07-25 00:00:009574499.70333
2013-07-25 00:00:003653359.44444
2013-07-25 00:00:0010732999.85555
2013-07-25 00:00:0010142798.88666
2013-07-25 00:00:004031949.85777
2013-07-25 00:00:005021650.00888
2013-07-25 00:00:006271079.73999
2013-07-25 00:00:00226599.99101010
2013-07-25 00:00:0024319.96111111
2013-07-25 00:00:00821207.96121212
2013-07-25 00:00:00625199.99131313
2013-07-25 00:00:00705119.99141414
2013-07-25 00:00:00572119.97151515
2013-07-25 00:00:00666109.99161616
2013-07-25 00:00:00725108.00171717
2013-07-25 00:00:00134100.00181818
2013-07-25 00:00:0090699.96191919
2013-07-25 00:00:0082895.97202020
2013-07-25 00:00:0081079.96212121
2013-07-25 00:00:0092479.95222222
2013-07-25 00:00:0092679.95222223
2013-07-25 00:00:009374.97242324
2013-07-25 00:00:0083563.98252425
2013-07-25 00:00:0089749.98262526
2013-07-26 00:00:00100410799.46111
2013-07-26 00:00:003657978.67222
2013-07-26 00:00:009576899.54333
2013-07-26 00:00:001916799.32444
" ], "text/plain": [ "[(datetime.datetime(2013, 7, 25, 0, 0), 1004, Decimal('5599.72'), 1, 1, 1),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 191, Decimal('5099.49'), 2, 2, 2),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 957, Decimal('4499.70'), 3, 3, 3),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 365, Decimal('3359.44'), 4, 4, 4),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 1073, Decimal('2999.85'), 5, 5, 5),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 1014, Decimal('2798.88'), 6, 6, 6),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 403, Decimal('1949.85'), 7, 7, 7),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 502, Decimal('1650.00'), 8, 8, 8),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 627, Decimal('1079.73'), 9, 9, 9),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 226, Decimal('599.99'), 10, 10, 10),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 24, Decimal('319.96'), 11, 11, 11),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 821, Decimal('207.96'), 12, 12, 12),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 625, Decimal('199.99'), 13, 13, 13),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 705, Decimal('119.99'), 14, 14, 14),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 572, Decimal('119.97'), 15, 15, 15),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 666, Decimal('109.99'), 16, 16, 16),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 725, Decimal('108.00'), 17, 17, 17),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 134, Decimal('100.00'), 18, 18, 18),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 906, Decimal('99.96'), 19, 19, 19),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 828, Decimal('95.97'), 20, 20, 20),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 810, Decimal('79.96'), 21, 21, 21),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 924, Decimal('79.95'), 22, 22, 22),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 926, Decimal('79.95'), 22, 22, 23),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 93, Decimal('74.97'), 24, 23, 24),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 835, Decimal('63.98'), 25, 24, 25),\n", " (datetime.datetime(2013, 7, 25, 0, 0), 897, Decimal('49.98'), 26, 25, 26),\n", " (datetime.datetime(2013, 7, 26, 0, 0), 1004, Decimal('10799.46'), 1, 1, 1),\n", " (datetime.datetime(2013, 7, 26, 0, 0), 365, Decimal('7978.67'), 2, 2, 2),\n", " (datetime.datetime(2013, 7, 26, 0, 0), 957, Decimal('6899.54'), 3, 3, 3),\n", " (datetime.datetime(2013, 7, 26, 0, 0), 191, Decimal('6799.32'), 4, 4, 4)]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%sql\n", "\n", "SELECT\n", " t.*,\n", " rank() OVER (\n", " PARTITION BY order_date\n", " ORDER BY revenue DESC\n", " ) rnk,\n", " dense_rank() OVER (\n", " PARTITION BY order_date\n", " ORDER BY revenue DESC\n", " ) drnk,\n", " row_number() OVER (\n", " PARTITION BY order_date\n", " ORDER BY revenue DESC\n", " ) rn\n", "FROM daily_product_revenue AS t\n", "ORDER BY order_date, revenue DESC\n", "LIMIT 30" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.12" } }, "nbformat": 4, "nbformat_minor": 4 }