Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions 01_gd_sql-LooqboxDataChallenge.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#1. What are the 10 most expensive products in the company?
SELECT PRODUCT_NAME, PRODUCT_VAL
FROM `looqbox-challenge`.data_product
ORDER BY PRODUCT_VAL DESC
LIMIT 10;

#2. What sections do the 'BEBIDAS' and 'PADARIA' departments have?
SELECT DISTINCT DEP_NAME , SECTION_NAME
FROM `looqbox-challenge`.data_product
WHERE DEP_NAME IN ('BEBIDAS','PADARIA')
ORDER BY DEP_NAME, SECTION_NAME;

#3. What was the total sale of products (in $) of each Business Area in the first quarter of 2019?
SELECT CAD.BUSINESS_NAME , SUM(SALES.SALES_VALUE) AS SALES_VALUE
FROM `looqbox-challenge`.data_store_sales AS SALES
LEFT JOIN `looqbox-challenge`.data_store_cad as CAD
ON SALES.STORE_CODE = CAD.STORE_CODE
WHERE SALES.DATE BETWEEN '2019-01-01' AND '2019-03-31'
GROUP BY CAD.BUSINESS_NAME
ORDER BY SUM(SALES.SALES_VALUE) DESC;


340 changes: 340 additions & 0 deletions 02-gd-Case1-datachallenge.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,340 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "5ade6e06",
"metadata": {},
"source": [
"### Case 1\n",
"#### 1) The Dev Team was tired of developing the same old queries just varying the filters accordingly to their boss demands.\n",
"As a new member of the crew, your mission now is to create a dynamic function in Python, on the most flexible of ways, to produce queries and retrieve a dataframe based on three parameters:\n",
"\n",
"- product_code: integer\n",
"\n",
"- store_code: integer\n",
"\n",
"- date: list of ISO-like strings\n",
"\n",
"- Date e.g.\n",
" - ['2019-01-01', '2019-01-31']\n",
"\n",
"It should look like this\n",
"my_data = retrieve_data(product_code, store_code, date)\n",
"\n",
"Make your team proud!\n",
"\n",
"Extra instructions:\n",
"- Retrieve all columns from table data_product_sales;\n",
"- Imagine people from other teams will also utilize this function!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3e772ed3",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from sqlalchemy import create_engine"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6959a8b8",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>STORE_CODE</th>\n",
" <th>PRODUCT_CODE</th>\n",
" <th>DATE</th>\n",
" <th>SALES_VALUE</th>\n",
" <th>SALES_QTY</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-01-01</td>\n",
" <td>708.5</td>\n",
" <td>65.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-01-02</td>\n",
" <td>1297.1</td>\n",
" <td>119.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-01-03</td>\n",
" <td>1144.5</td>\n",
" <td>105.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-01-04</td>\n",
" <td>1090.0</td>\n",
" <td>100.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-01-05</td>\n",
" <td>893.8</td>\n",
" <td>82.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" STORE_CODE PRODUCT_CODE DATE SALES_VALUE SALES_QTY\n",
"0 1 18 2019-01-01 708.5 65.0\n",
"1 1 18 2019-01-02 1297.1 119.0\n",
"2 1 18 2019-01-03 1144.5 105.0\n",
"3 1 18 2019-01-04 1090.0 100.0\n",
"4 1 18 2019-01-05 893.8 82.0"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def retrieve_data(product_code, store_code, date):\n",
" # 1. Credenciais de acesso\n",
" usuario = 'looqbox-challenge'\n",
" senha = 'looq-challenge'\n",
" host = '35.199.115.174'\n",
" porta = '3306' # porta padrão do MySQL\n",
" nome_do_banco = 'looqbox-challenge'\n",
"\n",
" # 2. String de conexão (URL de conexão)\n",
" # Formato: mysql+pymysql://usuario:senha@host:porta/nome_do_banco\n",
" string_conexao = f'mysql+pymysql://{usuario}:{senha}@{host}:{porta}/{nome_do_banco}'\n",
"\n",
" # 3. Motor de conexão (engine)\n",
" engine = create_engine(string_conexao)\n",
"\n",
" # 4. Consulta SQL\n",
" query = \"\"\"SELECT * FROM `looqbox-challenge`.`data_product_sales`\n",
" WHERE PRODUCT_CODE = %s\n",
" AND DATE BETWEEN %s AND %s\n",
" AND STORE_CODE = %s\"\"\"\n",
"\n",
" df = pd.read_sql(query, con=engine, params=(product_code, date[0], date[1], store_code))\n",
"\n",
" # 6. retorna o DataFrame\n",
" return df\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "361ee7b7",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>STORE_CODE</th>\n",
" <th>PRODUCT_CODE</th>\n",
" <th>DATE</th>\n",
" <th>SALES_VALUE</th>\n",
" <th>SALES_QTY</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-01-01</td>\n",
" <td>708.5</td>\n",
" <td>65.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-01-02</td>\n",
" <td>1297.1</td>\n",
" <td>119.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-01-03</td>\n",
" <td>1144.5</td>\n",
" <td>105.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-01-04</td>\n",
" <td>1090.0</td>\n",
" <td>100.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-01-05</td>\n",
" <td>893.8</td>\n",
" <td>82.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>85</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-03-27</td>\n",
" <td>1308.0</td>\n",
" <td>120.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>86</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-03-28</td>\n",
" <td>1079.1</td>\n",
" <td>99.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>87</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-03-29</td>\n",
" <td>1002.8</td>\n",
" <td>92.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>88</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-03-30</td>\n",
" <td>741.2</td>\n",
" <td>68.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>89</th>\n",
" <td>1</td>\n",
" <td>18</td>\n",
" <td>2019-03-31</td>\n",
" <td>741.2</td>\n",
" <td>68.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>90 rows × 5 columns</p>\n",
"</div>"
],
"text/plain": [
" STORE_CODE PRODUCT_CODE DATE SALES_VALUE SALES_QTY\n",
"0 1 18 2019-01-01 708.5 65.0\n",
"1 1 18 2019-01-02 1297.1 119.0\n",
"2 1 18 2019-01-03 1144.5 105.0\n",
"3 1 18 2019-01-04 1090.0 100.0\n",
"4 1 18 2019-01-05 893.8 82.0\n",
".. ... ... ... ... ...\n",
"85 1 18 2019-03-27 1308.0 120.0\n",
"86 1 18 2019-03-28 1079.1 99.0\n",
"87 1 18 2019-03-29 1002.8 92.0\n",
"88 1 18 2019-03-30 741.2 68.0\n",
"89 1 18 2019-03-31 741.2 68.0\n",
"\n",
"[90 rows x 5 columns]"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_data = retrieve_data(product_code=18, store_code=1, date=['2019-01-01', '2019-03-31'])\n",
"my_data"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading