{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Analytic Functions – Aggregations\n",
"\n",
"Let us see how we can perform aggregations with in a partition or group using Windowing/Analytics Functions."
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {
"tags": [
"remove-cell"
]
},
"outputs": [
{
"data": {
"text/html": [
"VIDEO \n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%HTML\n",
"VIDEO "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* For simple aggregations where we have to get grouping key and aggregated results we can use **GROUP BY**.\n",
"* If we want to get the raw data along with aggregated results, then using **GROUP BY** is not possible or overly complicated.\n",
"* Using aggregate functions with **OVER** Clause not only simplifies the process of writing query, but also better with respect to performance.\n",
"* Let us take an example of getting employee salary percentage when compared to department salary expense."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```{warning}\n",
"If you are using Jupyter based environment make sure to restart the kernel, as the session might have been already connected with 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",
" employee_id \n",
" department_id \n",
" salary \n",
" \n",
" \n",
" 200 \n",
" 10 \n",
" 4400.00 \n",
" \n",
" \n",
" 202 \n",
" 20 \n",
" 6000.00 \n",
" \n",
" \n",
" 201 \n",
" 20 \n",
" 13000.00 \n",
" \n",
" \n",
" 119 \n",
" 30 \n",
" 2500.00 \n",
" \n",
" \n",
" 118 \n",
" 30 \n",
" 2600.00 \n",
" \n",
" \n",
" 117 \n",
" 30 \n",
" 2800.00 \n",
" \n",
" \n",
" 116 \n",
" 30 \n",
" 2900.00 \n",
" \n",
" \n",
" 115 \n",
" 30 \n",
" 3100.00 \n",
" \n",
" \n",
" 114 \n",
" 30 \n",
" 11000.00 \n",
" \n",
" \n",
" 203 \n",
" 40 \n",
" 6500.00 \n",
" \n",
"
"
],
"text/plain": [
"[(200, 10, Decimal('4400.00')),\n",
" (202, 20, Decimal('6000.00')),\n",
" (201, 20, Decimal('13000.00')),\n",
" (119, 30, Decimal('2500.00')),\n",
" (118, 30, Decimal('2600.00')),\n",
" (117, 30, Decimal('2800.00')),\n",
" (116, 30, Decimal('2900.00')),\n",
" (115, 30, Decimal('3100.00')),\n",
" (114, 30, Decimal('11000.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 \n",
"FROM employees \n",
"ORDER BY department_id, salary\n",
"LIMIT 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```{note}\n",
"Let us write the query using `GROUP BY` approach.\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * postgresql://itversity_hr_user:***@localhost:5432/itversity_hr_db\n",
"12 rows affected.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" department_id \n",
" department_salary_expense \n",
" \n",
" \n",
" 10 \n",
" 4400.00 \n",
" \n",
" \n",
" 20 \n",
" 19000.00 \n",
" \n",
" \n",
" 30 \n",
" 24900.00 \n",
" \n",
" \n",
" 40 \n",
" 6500.00 \n",
" \n",
" \n",
" 50 \n",
" 156400.00 \n",
" \n",
" \n",
" 60 \n",
" 28800.00 \n",
" \n",
" \n",
" 70 \n",
" 10000.00 \n",
" \n",
" \n",
" 80 \n",
" 304500.00 \n",
" \n",
" \n",
" 90 \n",
" 58000.00 \n",
" \n",
" \n",
" 100 \n",
" 51600.00 \n",
" \n",
" \n",
" 110 \n",
" 20300.00 \n",
" \n",
" \n",
" None \n",
" 7000.00 \n",
" \n",
"
"
],
"text/plain": [
"[(10, Decimal('4400.00')),\n",
" (20, Decimal('19000.00')),\n",
" (30, Decimal('24900.00')),\n",
" (40, Decimal('6500.00')),\n",
" (50, Decimal('156400.00')),\n",
" (60, Decimal('28800.00')),\n",
" (70, Decimal('10000.00')),\n",
" (80, Decimal('304500.00')),\n",
" (90, Decimal('58000.00')),\n",
" (100, Decimal('51600.00')),\n",
" (110, Decimal('20300.00')),\n",
" (None, Decimal('7000.00'))]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"SELECT department_id,\n",
" sum(salary) AS department_salary_expense\n",
"FROM employees\n",
"GROUP BY department_id\n",
"ORDER BY department_id"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * postgresql://itversity_hr_user:***@localhost:5432/itversity_hr_db\n",
"10 rows affected.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" employee_id \n",
" department_id \n",
" salary \n",
" department_salary_expense \n",
" avg_salary_expense \n",
" \n",
" \n",
" 200 \n",
" 10 \n",
" 4400.00 \n",
" 4400.00 \n",
" 4400.00 \n",
" \n",
" \n",
" 202 \n",
" 20 \n",
" 6000.00 \n",
" 19000.00 \n",
" 9500.00 \n",
" \n",
" \n",
" 201 \n",
" 20 \n",
" 13000.00 \n",
" 19000.00 \n",
" 9500.00 \n",
" \n",
" \n",
" 119 \n",
" 30 \n",
" 2500.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" \n",
" \n",
" 118 \n",
" 30 \n",
" 2600.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" \n",
" \n",
" 117 \n",
" 30 \n",
" 2800.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" \n",
" \n",
" 116 \n",
" 30 \n",
" 2900.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" \n",
" \n",
" 115 \n",
" 30 \n",
" 3100.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" \n",
" \n",
" 114 \n",
" 30 \n",
" 11000.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" \n",
" \n",
" 203 \n",
" 40 \n",
" 6500.00 \n",
" 6500.00 \n",
" 6500.00 \n",
" \n",
"
"
],
"text/plain": [
"[(200, 10, Decimal('4400.00'), Decimal('4400.00'), Decimal('4400.00')),\n",
" (202, 20, Decimal('6000.00'), Decimal('19000.00'), Decimal('9500.00')),\n",
" (201, 20, Decimal('13000.00'), Decimal('19000.00'), Decimal('9500.00')),\n",
" (119, 30, Decimal('2500.00'), Decimal('24900.00'), Decimal('4150.00')),\n",
" (118, 30, Decimal('2600.00'), Decimal('24900.00'), Decimal('4150.00')),\n",
" (117, 30, Decimal('2800.00'), Decimal('24900.00'), Decimal('4150.00')),\n",
" (116, 30, Decimal('2900.00'), Decimal('24900.00'), Decimal('4150.00')),\n",
" (115, 30, Decimal('3100.00'), Decimal('24900.00'), Decimal('4150.00')),\n",
" (114, 30, Decimal('11000.00'), Decimal('24900.00'), Decimal('4150.00')),\n",
" (203, 40, Decimal('6500.00'), Decimal('6500.00'), Decimal('6500.00'))]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"SELECT e.employee_id, e.department_id, e.salary,\n",
" ae.department_salary_expense,\n",
" ae.avg_salary_expense\n",
"FROM employees e JOIN (\n",
" SELECT department_id, \n",
" sum(salary) AS department_salary_expense,\n",
" round(avg(salary)::numeric, 2) AS avg_salary_expense\n",
" FROM employees\n",
" GROUP BY department_id\n",
") ae\n",
"ON e.department_id = ae.department_id\n",
"ORDER BY department_id, salary\n",
"LIMIT 10"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * postgresql://itversity_hr_user:***@localhost:5432/itversity_hr_db\n",
"10 rows affected.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" employee_id \n",
" department_id \n",
" salary \n",
" department_salary_expense \n",
" avg_salary_expense \n",
" pct_salary \n",
" \n",
" \n",
" 200 \n",
" 10 \n",
" 4400.00 \n",
" 4400.00 \n",
" 4400.00 \n",
" 100.00 \n",
" \n",
" \n",
" 202 \n",
" 20 \n",
" 6000.00 \n",
" 19000.00 \n",
" 9500.00 \n",
" 31.58 \n",
" \n",
" \n",
" 201 \n",
" 20 \n",
" 13000.00 \n",
" 19000.00 \n",
" 9500.00 \n",
" 68.42 \n",
" \n",
" \n",
" 119 \n",
" 30 \n",
" 2500.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" 10.04 \n",
" \n",
" \n",
" 118 \n",
" 30 \n",
" 2600.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" 10.44 \n",
" \n",
" \n",
" 117 \n",
" 30 \n",
" 2800.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" 11.24 \n",
" \n",
" \n",
" 116 \n",
" 30 \n",
" 2900.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" 11.65 \n",
" \n",
" \n",
" 115 \n",
" 30 \n",
" 3100.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" 12.45 \n",
" \n",
" \n",
" 114 \n",
" 30 \n",
" 11000.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" 44.18 \n",
" \n",
" \n",
" 203 \n",
" 40 \n",
" 6500.00 \n",
" 6500.00 \n",
" 6500.00 \n",
" 100.00 \n",
" \n",
"
"
],
"text/plain": [
"[(200, 10, Decimal('4400.00'), Decimal('4400.00'), Decimal('4400.00'), Decimal('100.00')),\n",
" (202, 20, Decimal('6000.00'), Decimal('19000.00'), Decimal('9500.00'), Decimal('31.58')),\n",
" (201, 20, Decimal('13000.00'), Decimal('19000.00'), Decimal('9500.00'), Decimal('68.42')),\n",
" (119, 30, Decimal('2500.00'), Decimal('24900.00'), Decimal('4150.00'), Decimal('10.04')),\n",
" (118, 30, Decimal('2600.00'), Decimal('24900.00'), Decimal('4150.00'), Decimal('10.44')),\n",
" (117, 30, Decimal('2800.00'), Decimal('24900.00'), Decimal('4150.00'), Decimal('11.24')),\n",
" (116, 30, Decimal('2900.00'), Decimal('24900.00'), Decimal('4150.00'), Decimal('11.65')),\n",
" (115, 30, Decimal('3100.00'), Decimal('24900.00'), Decimal('4150.00'), Decimal('12.45')),\n",
" (114, 30, Decimal('11000.00'), Decimal('24900.00'), Decimal('4150.00'), Decimal('44.18')),\n",
" (203, 40, Decimal('6500.00'), Decimal('6500.00'), Decimal('6500.00'), Decimal('100.00'))]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"SELECT e.employee_id, e.department_id, e.salary,\n",
" ae.department_salary_expense,\n",
" ae.avg_salary_expense,\n",
" round(e.salary/ae.department_salary_expense * 100, 2) pct_salary\n",
"FROM employees e JOIN (\n",
" SELECT department_id, \n",
" sum(salary) AS department_salary_expense,\n",
" round(avg(salary)::numeric, 2) AS avg_salary_expense\n",
" FROM employees\n",
" GROUP BY department_id\n",
") ae\n",
"ON e.department_id = ae.department_id\n",
"ORDER BY department_id, salary\n",
"LIMIT 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```{note}\n",
"Let us see how we can get it using Analytics/Windowing Functions. \n",
"```\n",
"\n",
"* We can use all standard aggregate functions such as `count`, `sum`, `min`, `max`, `avg` etc."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * postgresql://itversity_hr_user:***@localhost:5432/itversity_hr_db\n",
"10 rows affected.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" employee_id \n",
" department_id \n",
" salary \n",
" department_salary_expense \n",
" \n",
" \n",
" 200 \n",
" 10 \n",
" 4400.00 \n",
" 4400.00 \n",
" \n",
" \n",
" 201 \n",
" 20 \n",
" 13000.00 \n",
" 19000.00 \n",
" \n",
" \n",
" 202 \n",
" 20 \n",
" 6000.00 \n",
" 19000.00 \n",
" \n",
" \n",
" 114 \n",
" 30 \n",
" 11000.00 \n",
" 24900.00 \n",
" \n",
" \n",
" 115 \n",
" 30 \n",
" 3100.00 \n",
" 24900.00 \n",
" \n",
" \n",
" 116 \n",
" 30 \n",
" 2900.00 \n",
" 24900.00 \n",
" \n",
" \n",
" 117 \n",
" 30 \n",
" 2800.00 \n",
" 24900.00 \n",
" \n",
" \n",
" 118 \n",
" 30 \n",
" 2600.00 \n",
" 24900.00 \n",
" \n",
" \n",
" 119 \n",
" 30 \n",
" 2500.00 \n",
" 24900.00 \n",
" \n",
" \n",
" 203 \n",
" 40 \n",
" 6500.00 \n",
" 6500.00 \n",
" \n",
"
"
],
"text/plain": [
"[(200, 10, Decimal('4400.00'), Decimal('4400.00')),\n",
" (201, 20, Decimal('13000.00'), Decimal('19000.00')),\n",
" (202, 20, Decimal('6000.00'), Decimal('19000.00')),\n",
" (114, 30, Decimal('11000.00'), Decimal('24900.00')),\n",
" (115, 30, Decimal('3100.00'), Decimal('24900.00')),\n",
" (116, 30, Decimal('2900.00'), Decimal('24900.00')),\n",
" (117, 30, Decimal('2800.00'), Decimal('24900.00')),\n",
" (118, 30, Decimal('2600.00'), Decimal('24900.00')),\n",
" (119, 30, Decimal('2500.00'), Decimal('24900.00')),\n",
" (203, 40, Decimal('6500.00'), Decimal('6500.00'))]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"SELECT e.employee_id, e.department_id, e.salary,\n",
" sum(e.salary) OVER (\n",
" PARTITION BY e.department_id\n",
" ) AS department_salary_expense\n",
"FROM employees e\n",
"ORDER BY e.department_id\n",
"LIMIT 10"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * postgresql://itversity_hr_user:***@localhost:5432/itversity_hr_db\n",
"10 rows affected.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" employee_id \n",
" department_id \n",
" salary \n",
" department_salary_expense \n",
" pct_salary \n",
" \n",
" \n",
" 200 \n",
" 10 \n",
" 4400.00 \n",
" 4400.00 \n",
" 100.00 \n",
" \n",
" \n",
" 202 \n",
" 20 \n",
" 6000.00 \n",
" 19000.00 \n",
" 31.58 \n",
" \n",
" \n",
" 201 \n",
" 20 \n",
" 13000.00 \n",
" 19000.00 \n",
" 68.42 \n",
" \n",
" \n",
" 119 \n",
" 30 \n",
" 2500.00 \n",
" 24900.00 \n",
" 10.04 \n",
" \n",
" \n",
" 118 \n",
" 30 \n",
" 2600.00 \n",
" 24900.00 \n",
" 10.44 \n",
" \n",
" \n",
" 117 \n",
" 30 \n",
" 2800.00 \n",
" 24900.00 \n",
" 11.24 \n",
" \n",
" \n",
" 116 \n",
" 30 \n",
" 2900.00 \n",
" 24900.00 \n",
" 11.65 \n",
" \n",
" \n",
" 115 \n",
" 30 \n",
" 3100.00 \n",
" 24900.00 \n",
" 12.45 \n",
" \n",
" \n",
" 114 \n",
" 30 \n",
" 11000.00 \n",
" 24900.00 \n",
" 44.18 \n",
" \n",
" \n",
" 203 \n",
" 40 \n",
" 6500.00 \n",
" 6500.00 \n",
" 100.00 \n",
" \n",
"
"
],
"text/plain": [
"[(200, 10, Decimal('4400.00'), Decimal('4400.00'), Decimal('100.00')),\n",
" (202, 20, Decimal('6000.00'), Decimal('19000.00'), Decimal('31.58')),\n",
" (201, 20, Decimal('13000.00'), Decimal('19000.00'), Decimal('68.42')),\n",
" (119, 30, Decimal('2500.00'), Decimal('24900.00'), Decimal('10.04')),\n",
" (118, 30, Decimal('2600.00'), Decimal('24900.00'), Decimal('10.44')),\n",
" (117, 30, Decimal('2800.00'), Decimal('24900.00'), Decimal('11.24')),\n",
" (116, 30, Decimal('2900.00'), Decimal('24900.00'), Decimal('11.65')),\n",
" (115, 30, Decimal('3100.00'), Decimal('24900.00'), Decimal('12.45')),\n",
" (114, 30, Decimal('11000.00'), Decimal('24900.00'), Decimal('44.18')),\n",
" (203, 40, Decimal('6500.00'), Decimal('6500.00'), Decimal('100.00'))]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"SELECT e.employee_id, e.department_id, e.salary,\n",
" sum(e.salary) OVER (\n",
" PARTITION BY e.department_id\n",
" ) AS department_salary_expense,\n",
" round(e.salary / sum(e.salary) OVER (\n",
" PARTITION BY e.department_id\n",
" ) * 100, 2) AS pct_salary\n",
"FROM employees e\n",
"ORDER BY e.department_id,\n",
" e.salary\n",
"LIMIT 10"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * postgresql://itversity_hr_user:***@localhost:5432/itversity_hr_db\n",
"10 rows affected.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" employee_id \n",
" department_id \n",
" salary \n",
" sum_sal_expense \n",
" avg_sal_expense \n",
" min_sal_expense \n",
" max_sal_expense \n",
" cnt_sal_expense \n",
" \n",
" \n",
" 200 \n",
" 10 \n",
" 4400.00 \n",
" 4400.00 \n",
" 4400.00 \n",
" 4400.00 \n",
" 4400.00 \n",
" 1 \n",
" \n",
" \n",
" 202 \n",
" 20 \n",
" 6000.00 \n",
" 19000.00 \n",
" 9500.00 \n",
" 6000.00 \n",
" 13000.00 \n",
" 2 \n",
" \n",
" \n",
" 201 \n",
" 20 \n",
" 13000.00 \n",
" 19000.00 \n",
" 9500.00 \n",
" 6000.00 \n",
" 13000.00 \n",
" 2 \n",
" \n",
" \n",
" 119 \n",
" 30 \n",
" 2500.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" 2500.00 \n",
" 11000.00 \n",
" 6 \n",
" \n",
" \n",
" 118 \n",
" 30 \n",
" 2600.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" 2500.00 \n",
" 11000.00 \n",
" 6 \n",
" \n",
" \n",
" 117 \n",
" 30 \n",
" 2800.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" 2500.00 \n",
" 11000.00 \n",
" 6 \n",
" \n",
" \n",
" 116 \n",
" 30 \n",
" 2900.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" 2500.00 \n",
" 11000.00 \n",
" 6 \n",
" \n",
" \n",
" 115 \n",
" 30 \n",
" 3100.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" 2500.00 \n",
" 11000.00 \n",
" 6 \n",
" \n",
" \n",
" 114 \n",
" 30 \n",
" 11000.00 \n",
" 24900.00 \n",
" 4150.00 \n",
" 2500.00 \n",
" 11000.00 \n",
" 6 \n",
" \n",
" \n",
" 203 \n",
" 40 \n",
" 6500.00 \n",
" 6500.00 \n",
" 6500.00 \n",
" 6500.00 \n",
" 6500.00 \n",
" 1 \n",
" \n",
"
"
],
"text/plain": [
"[(200, 10, Decimal('4400.00'), Decimal('4400.00'), Decimal('4400.00'), Decimal('4400.00'), Decimal('4400.00'), 1),\n",
" (202, 20, Decimal('6000.00'), Decimal('19000.00'), Decimal('9500.00'), Decimal('6000.00'), Decimal('13000.00'), 2),\n",
" (201, 20, Decimal('13000.00'), Decimal('19000.00'), Decimal('9500.00'), Decimal('6000.00'), Decimal('13000.00'), 2),\n",
" (119, 30, Decimal('2500.00'), Decimal('24900.00'), Decimal('4150.00'), Decimal('2500.00'), Decimal('11000.00'), 6),\n",
" (118, 30, Decimal('2600.00'), Decimal('24900.00'), Decimal('4150.00'), Decimal('2500.00'), Decimal('11000.00'), 6),\n",
" (117, 30, Decimal('2800.00'), Decimal('24900.00'), Decimal('4150.00'), Decimal('2500.00'), Decimal('11000.00'), 6),\n",
" (116, 30, Decimal('2900.00'), Decimal('24900.00'), Decimal('4150.00'), Decimal('2500.00'), Decimal('11000.00'), 6),\n",
" (115, 30, Decimal('3100.00'), Decimal('24900.00'), Decimal('4150.00'), Decimal('2500.00'), Decimal('11000.00'), 6),\n",
" (114, 30, Decimal('11000.00'), Decimal('24900.00'), Decimal('4150.00'), Decimal('2500.00'), Decimal('11000.00'), 6),\n",
" (203, 40, Decimal('6500.00'), Decimal('6500.00'), Decimal('6500.00'), Decimal('6500.00'), Decimal('6500.00'), 1)]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"SELECT e.employee_id, e.department_id, e.salary,\n",
" sum(e.salary) OVER (\n",
" PARTITION BY e.department_id\n",
" ) AS sum_sal_expense,\n",
" round(avg(e.salary) OVER (\n",
" PARTITION BY e.department_id\n",
" ), 2) AS avg_sal_expense,\n",
" min(e.salary) OVER (\n",
" PARTITION BY e.department_id\n",
" ) AS min_sal_expense,\n",
" max(e.salary) OVER (\n",
" PARTITION BY e.department_id\n",
" ) AS max_sal_expense,\n",
" count(e.salary) OVER (\n",
" PARTITION BY e.department_id\n",
" ) AS cnt_sal_expense\n",
"FROM employees e\n",
"ORDER BY e.department_id,\n",
" e.salary\n",
"LIMIT 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```{warning}\n",
"If you are using Jupyter based environment make sure to restart the kernel, as the session might have been already connected with 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": [
"10 rows affected.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" order_date \n",
" order_item_product_id \n",
" revenue \n",
" sum_revenue \n",
" min_revenue \n",
" max_revenue \n",
" \n",
" \n",
" 2013-07-25 00:00:00 \n",
" 1004 \n",
" 5599.72 \n",
" 31547.23 \n",
" 49.98 \n",
" 5599.72 \n",
" \n",
" \n",
" 2013-07-25 00:00:00 \n",
" 191 \n",
" 5099.49 \n",
" 31547.23 \n",
" 49.98 \n",
" 5599.72 \n",
" \n",
" \n",
" 2013-07-25 00:00:00 \n",
" 957 \n",
" 4499.70 \n",
" 31547.23 \n",
" 49.98 \n",
" 5599.72 \n",
" \n",
" \n",
" 2013-07-25 00:00:00 \n",
" 365 \n",
" 3359.44 \n",
" 31547.23 \n",
" 49.98 \n",
" 5599.72 \n",
" \n",
" \n",
" 2013-07-25 00:00:00 \n",
" 1073 \n",
" 2999.85 \n",
" 31547.23 \n",
" 49.98 \n",
" 5599.72 \n",
" \n",
" \n",
" 2013-07-25 00:00:00 \n",
" 1014 \n",
" 2798.88 \n",
" 31547.23 \n",
" 49.98 \n",
" 5599.72 \n",
" \n",
" \n",
" 2013-07-25 00:00:00 \n",
" 403 \n",
" 1949.85 \n",
" 31547.23 \n",
" 49.98 \n",
" 5599.72 \n",
" \n",
" \n",
" 2013-07-25 00:00:00 \n",
" 502 \n",
" 1650.00 \n",
" 31547.23 \n",
" 49.98 \n",
" 5599.72 \n",
" \n",
" \n",
" 2013-07-25 00:00:00 \n",
" 627 \n",
" 1079.73 \n",
" 31547.23 \n",
" 49.98 \n",
" 5599.72 \n",
" \n",
" \n",
" 2013-07-25 00:00:00 \n",
" 226 \n",
" 599.99 \n",
" 31547.23 \n",
" 49.98 \n",
" 5599.72 \n",
" \n",
"
"
],
"text/plain": [
"[(datetime.datetime(2013, 7, 25, 0, 0), 1004, Decimal('5599.72'), Decimal('31547.23'), Decimal('49.98'), Decimal('5599.72')),\n",
" (datetime.datetime(2013, 7, 25, 0, 0), 191, Decimal('5099.49'), Decimal('31547.23'), Decimal('49.98'), Decimal('5599.72')),\n",
" (datetime.datetime(2013, 7, 25, 0, 0), 957, Decimal('4499.70'), Decimal('31547.23'), Decimal('49.98'), Decimal('5599.72')),\n",
" (datetime.datetime(2013, 7, 25, 0, 0), 365, Decimal('3359.44'), Decimal('31547.23'), Decimal('49.98'), Decimal('5599.72')),\n",
" (datetime.datetime(2013, 7, 25, 0, 0), 1073, Decimal('2999.85'), Decimal('31547.23'), Decimal('49.98'), Decimal('5599.72')),\n",
" (datetime.datetime(2013, 7, 25, 0, 0), 1014, Decimal('2798.88'), Decimal('31547.23'), Decimal('49.98'), Decimal('5599.72')),\n",
" (datetime.datetime(2013, 7, 25, 0, 0), 403, Decimal('1949.85'), Decimal('31547.23'), Decimal('49.98'), Decimal('5599.72')),\n",
" (datetime.datetime(2013, 7, 25, 0, 0), 502, Decimal('1650.00'), Decimal('31547.23'), Decimal('49.98'), Decimal('5599.72')),\n",
" (datetime.datetime(2013, 7, 25, 0, 0), 627, Decimal('1079.73'), Decimal('31547.23'), Decimal('49.98'), Decimal('5599.72')),\n",
" (datetime.datetime(2013, 7, 25, 0, 0), 226, Decimal('599.99'), Decimal('31547.23'), Decimal('49.98'), Decimal('5599.72'))]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"SELECT\n",
" order_date,\n",
" order_item_product_id,\n",
" revenue,\n",
" sum(revenue) OVER (PARTITION BY order_date) AS sum_revenue,\n",
" min(revenue) OVER (PARTITION BY order_date) AS min_revenue,\n",
" max(revenue) OVER (PARTITION BY order_date) AS max_revenue\n",
"FROM daily_product_revenue\n",
"ORDER BY order_date,\n",
" revenue DESC\n",
"LIMIT 10"
]
}
],
"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
}