From 7a94f938b9914e030f107dd7c644aa140c3bd5ad Mon Sep 17 00:00:00 2001 From: pedro-gui-afonso Date: Sat, 2 Dec 2023 11:47:15 +0000 Subject: [PATCH 1/2] [lab-sql-9]Pedro --- lab-sql-9.sql | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 lab-sql-9.sql diff --git a/lab-sql-9.sql b/lab-sql-9.sql new file mode 100644 index 0000000..b7e5afb --- /dev/null +++ b/lab-sql-9.sql @@ -0,0 +1,20 @@ +create temporary table sakila.rentals_may as select * from sakila.rental where month(rental_date)=5; +create temporary table sakila.rentals_june as select * from sakila.rental where month(rental_date)=6; + +select concat(cust.first_name, " ", cust.last_name) as customer_name, count(may.rental_id) as nr_rentals_may +from sakila.customer cust left join sakila.rentals_may may +on may.customer_id = cust.customer_id +group by cust.customer_id order by 2 desc; + +create table sakila.rentals_may_june +as select cust.customer_id, concat(cust.first_name, " ", cust.last_name) as customer_name, +count(distinct may.rental_id) as nr_rentals_may, count(distinct june.rental_id) as nr_rentals_june +from +sakila.customer as cust +left join sakila.rentals_may as may +on may.customer_id = cust.customer_id +left join sakila.rentals_june as june +on june.customer_id = cust.customer_id +group by cust.customer_id order by (nr_rentals_may + nr_rentals_june) desc; + +select * from sakila.rentals_may_june; \ No newline at end of file From cf091be5fc39a573ed6a0276d1f77fff9bf4f3e4 Mon Sep 17 00:00:00 2001 From: pedro-gui-afonso Date: Sat, 2 Dec 2023 12:15:46 +0000 Subject: [PATCH 2/2] [lab-sql-9]Pedro --- lab-sql-9.ipynb | 388 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 388 insertions(+) create mode 100644 lab-sql-9.ipynb diff --git a/lab-sql-9.ipynb b/lab-sql-9.ipynb new file mode 100644 index 0000000..9ff620b --- /dev/null +++ b/lab-sql-9.ipynb @@ -0,0 +1,388 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "de221430", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\n!pip install mysql.connector\\n!pip install mysql.connector.python\\n!pip install sqlalchemy\\n!pip install pymysql\\n\\n'" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'''\n", + "!pip install mysql.connector\n", + "!pip install mysql.connector.python\n", + "!pip install sqlalchemy\n", + "!pip install pymysql\n", + "\n", + "'''" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "37d20b1b", + "metadata": {}, + "outputs": [], + "source": [ + "import pymysql\n", + "from sqlalchemy import create_engine\n", + "import pandas as pd\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "b4268fa3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "········\n" + ] + } + ], + "source": [ + "import getpass\n", + "password = getpass.getpass()" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "1639585c", + "metadata": {}, + "outputs": [ + { + "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", + "
customer_namenr_rentals_maynr_rentals_june
customer_id
197SUE PETERS88
176JUNE CARROLL58
371BILLY POULIN67
109EDNA WEST75
196ALMA AUSTIN48
............
440BERNARD COLBY10
487HECTOR POINDEXTER01
555DWIGHT LOMBARDI01
598WADE DELVALLE01
195VANESSA SIMS00
\n", + "

599 rows × 3 columns

\n", + "
" + ], + "text/plain": [ + " customer_name nr_rentals_may nr_rentals_june\n", + "customer_id \n", + "197 SUE PETERS 8 8\n", + "176 JUNE CARROLL 5 8\n", + "371 BILLY POULIN 6 7\n", + "109 EDNA WEST 7 5\n", + "196 ALMA AUSTIN 4 8\n", + "... ... ... ...\n", + "440 BERNARD COLBY 1 0\n", + "487 HECTOR POINDEXTER 0 1\n", + "555 DWIGHT LOMBARDI 0 1\n", + "598 WADE DELVALLE 0 1\n", + "195 VANESSA SIMS 0 0\n", + "\n", + "[599 rows x 3 columns]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "connection_string = 'mysql+pymysql://root:' + password + '@localhost/bank'\n", + "engine = create_engine(connection_string)\n", + "data = pd.read_sql_query('select * from sakila.rentals_may_june', engine)\n", + "data = data.set_index('customer_id')\n", + "data" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "6eae4c81", + "metadata": {}, + "outputs": [], + "source": [ + "def compare_rentals_may_june(df):\n", + " df['most_rentals'] = np.where(df['nr_rentals_may'] > df['nr_rentals_june'], 'may',\n", + " np.where(df['nr_rentals_may'] < df['nr_rentals_june'], 'june', 'equal'))\n", + " return df" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "0e35377b", + "metadata": {}, + "outputs": [ + { + "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", + "
customer_namenr_rentals_maynr_rentals_junemost_rentals
customer_id
197SUE PETERS88equal
176JUNE CARROLL58june
371BILLY POULIN67june
109EDNA WEST75may
196ALMA AUSTIN48june
...............
440BERNARD COLBY10may
487HECTOR POINDEXTER01june
555DWIGHT LOMBARDI01june
598WADE DELVALLE01june
195VANESSA SIMS00equal
\n", + "

599 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " customer_name nr_rentals_may nr_rentals_june most_rentals\n", + "customer_id \n", + "197 SUE PETERS 8 8 equal\n", + "176 JUNE CARROLL 5 8 june\n", + "371 BILLY POULIN 6 7 june\n", + "109 EDNA WEST 7 5 may\n", + "196 ALMA AUSTIN 4 8 june\n", + "... ... ... ... ...\n", + "440 BERNARD COLBY 1 0 may\n", + "487 HECTOR POINDEXTER 0 1 june\n", + "555 DWIGHT LOMBARDI 0 1 june\n", + "598 WADE DELVALLE 0 1 june\n", + "195 VANESSA SIMS 0 0 equal\n", + "\n", + "[599 rows x 4 columns]" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "compare_rentals_may_june(data)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}