From eecd91f85d753bce91269f01ea12aa660c5b0150 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Santanch=C3=A8?= Date: Wed, 6 Sep 2023 16:08:08 -0300 Subject: [PATCH] feat (exercises): title fixed --- exercises/README.md | 2 +- .../food-intake-analysis-results.ipynb | 343 ---- sql/food-intake/food-intake-basics.ipynb | 1760 ----------------- sql/food-intake/notebook.tex | 497 ----- 4 files changed, 1 insertion(+), 2601 deletions(-) delete mode 100644 sql/food-intake/food-intake-analysis-results.ipynb delete mode 100644 sql/food-intake/food-intake-basics.ipynb delete mode 100644 sql/food-intake/notebook.tex diff --git a/exercises/README.md b/exercises/README.md index 3724f82..4113ea3 100644 --- a/exercises/README.md +++ b/exercises/README.md @@ -5,5 +5,5 @@ ### SELECT básico (com JOIN implícito) e agrupamento * [Estradas](../sql/estradas/exercicios) - * Exercícios de 1 a 5 + * Notebook `estradas-03-sql.ipynb`, exercícios de 1 a 5 * [resolução](../sql/estradas/resolucao) \ No newline at end of file diff --git a/sql/food-intake/food-intake-analysis-results.ipynb b/sql/food-intake/food-intake-analysis-results.ipynb deleted file mode 100644 index 8f9b63f..0000000 --- a/sql/food-intake/food-intake-analysis-results.ipynb +++ /dev/null @@ -1,343 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# U.S. EPA Food Commodity Intake Database (FCID)\n", - "## [https://fcid.foodrisk.org/](https://fcid.foodrisk.org/)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ativando uma conexão de banco de dados em memória usando o SGBD H2:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "%defaultDatasource jdbc:h2:mem:db" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Importando Tabelas do FCID" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "DROP TABLE IF EXISTS Crop_Group;\n", - "DROP TABLE IF EXISTS FCID_Description;\n", - "DROP TABLE IF EXISTS Recipes;\n", - "DROP TABLE IF EXISTS Intake;\n", - "\n", - "CREATE TABLE Crop_Group (\n", - " CGN VARCHAR(2),\n", - " CGL VARCHAR(6),\n", - " Crop_Group_Description VARCHAR(80),\n", - " PRIMARY KEY (CGL)\n", - ") AS SELECT\n", - " CGN, CGL, Crop_Group_Description\n", - "FROM CSVREAD('../../data/food-intake/basics/FCID_Cropgroup_Description.csv');\n", - "\n", - "CREATE TABLE FCID_Description (\n", - " CGN VARCHAR(2),\n", - " CG_Subgroup VARCHAR(6),\n", - " FCID_Code VARCHAR(10),\n", - " FCID_Desc VARCHAR(55),\n", - " PRIMARY KEY (FCID_Code),\n", - ") AS SELECT\n", - " cgn, CG_Subgroup, FCID_Code, FCID_Desc\n", - "FROM CSVREAD('../../data/food-intake/basics/FCID_Code_Description.csv');\n", - "\n", - "CREATE TABLE Recipes (\n", - " Food_Code VARCHAR(8),\n", - " Mod_Code VARCHAR(8),\n", - " Ingredient_Num TINYINT,\n", - " FCID_Code VARCHAR(10),\n", - " Cooked_Status TINYINT,\n", - " Food_Form TINYINT,\n", - " Cooking_Method TINYINT,\n", - " Commodity_Weight DECIMAL(5, 2),\n", - " CSFII_9498_IND TINYINT,\n", - " WWEIA_9904_IND TINYINT,\n", - " WWEIA_0510_IND TINYINT,\n", - " PRIMARY KEY(Food_Code, Mod_Code, Ingredient_Num),\n", - " FOREIGN KEY(FCID_Code)\n", - " REFERENCES FCID_Description(FCID_Code)\n", - " ON DELETE NO ACTION\n", - " ON UPDATE NO ACTION\n", - ") AS SELECT\n", - " Food_Code, Mod_Code, Ingredient_Num, FCID_Code, Cooked_Status, Food_Form, Cooking_Method,\n", - " Commodity_Weight, CSFII_9498_IND, WWEIA_9904_IND, WWEIA_0510_IND\n", - "FROM CSVREAD('../../data/food-intake/recipes/Recipes_WWEIA_FCID_0510.csv');\n", - "\n", - "CREATE TABLE Intake (\n", - " SeqN INTEGER NOT NULL,\n", - " DayCode TINYINT NOT NULL,\n", - " DraBF TINYINT,\n", - " FCID_Code VARCHAR(10),\n", - " Cooked_Status TINYINT,\n", - " Food_Form TINYINT,\n", - " Cooking_Method TINYINT,\n", - " Intake DECIMAL(13,7),\n", - " Intake_BW DECIMAL(13,10),\n", - " PRIMARY KEY(SeqN, DayCode, FCID_Code, Cooked_Status, Food_Form, Cooking_Method),\n", - " FOREIGN KEY(FCID_Code)\n", - " REFERENCES FCID_Description(FCID_Code)\n", - " ON DELETE NO ACTION\n", - " ON UPDATE NO ACTION\n", - ") AS SELECT\n", - " SEQN, DAYCODE, DRABF, FCID_Code, Cooked_Status, Food_Form, Cooking_Method, Intake,Intake_BW\n", - "FROM CSVREAD('../../data/food-intake/consumption/Commodity_CSFFM_Intake_0510-cropped.csv');" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Visualizando as Tabelas" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "e3001587-9733-46d6-b995-7f174bf3380c", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT * FROM FCID_Description LIMIT 10;" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "37ecd0fb-e078-4d17-8ce0-a4d1430dd045", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT * FROM Recipes LIMIT 10;" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "7c49ed8c-f697-4f3e-8834-57271fe80096", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT * FROM Intake LIMIT 10;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 1) Apresentando uma Receita\n", - "\n", - "* Liste os ingredientes da receita de código `27111300` - Mexican style beef stew, no potatoes, tomato-based sauce (mixture).\n", - "* Não devem aparecer as modificações da receita.\n", - "* Mostre apenas o código da receita, o código de cada ingrediente, sua ordem e a participação no peso." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "d7d341d6-05bc-4000-8110-f6a4ab42ff8e", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Food_Code, FCID_Code, Ingredient_Num, Commodity_Weight\n", - "FROM Recipes\n", - "WHERE Food_Code = '27111300' AND Mod_Code = 0;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 2) Receita com Nomes de Ingredientes\n", - "* Aprimore a solução (1) para apresentar o nome dos ingredientes junto com seus códigos." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "dc51825b-ec0c-4002-a6f5-b56459e7a5fc", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT R.Food_Code, R.FCID_Code, F.FCID_Desc, R.Ingredient_Num, R.Commodity_Weight\n", - "FROM Recipes R, FCID_Description F\n", - "WHERE R.FCID_Code = F.FCID_Code AND R.Food_Code = '27111300' AND R.Mod_Code = 0;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 3) Grupos Alimentares da Receita\n", - "* A partir da receita escolhida em (1), apresente os grupos alimentares dos ingredientes contidos na receita.\n", - "* Cada grupo alimentar só deve aparecer uma vez no resultado.\n", - "* Para se obter o nome do grupo alimentar na tabela `Crop_Group` (não o subgrupo) devem ser considerados os registros em que a coluna `CGN` é igual à `CGL`." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "2befedac-5914-46fd-acc8-f2f1ec0ab00d", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT DISTINCT C.Crop_Group_Description\n", - "FROM Recipes R, FCID_Description F, Crop_Group C\n", - "WHERE R.FCID_Code = F.FCID_Code AND F.CGN = C.CGL AND R.Food_Code = '27111300' AND R.Mod_Code = 0;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 4) Participação nas Receitas\n", - "* Liste o nome de cada um dos produtos alimentares seguido do número de receitas em que ele participa.\n", - "* A lista deve ser apresentada em ordem decrescente de número de participação em receitas." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 5) Consumo Médio de Grupos de Alimentos\n", - "* Para cada Grupo de Alimentos (cada CGN diferente na tabela Crop_Group), apresente sua descrição e o consumo médio deste grupo de alimentos.\n", - "* O consumo é definido pelo campo `Intake` da tabela `Intake`." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "SQL", - "language": "SQL", - "name": "sql" - }, - "language_info": { - "codemirror_mode": "sql", - "file_extension": ".sql", - "mimetype": "", - "name": "SQL", - "nbconverter_exporter": "", - "version": "" - }, - "toc": { - "base_numbering": 1, - "nav_menu": {}, - "number_sections": false, - "sideBar": false, - "skip_h1_title": false, - "title_cell": "Table of Contents", - "title_sidebar": "Contents", - "toc_cell": false, - "toc_position": {}, - "toc_section_display": false, - "toc_window_display": false - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/sql/food-intake/food-intake-basics.ipynb b/sql/food-intake/food-intake-basics.ipynb deleted file mode 100644 index 245208b..0000000 --- a/sql/food-intake/food-intake-basics.ipynb +++ /dev/null @@ -1,1760 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Food Intake \n", - "\n", - "Ativando uma conexão de banco de dados em memória usando o SGBD H2:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "%defaultDatasource jdbc:h2:mem:db" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Conjunto de tabelas para consultas básicas" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "DROP TABLE IF EXISTS Intake;\n", - "\n", - "CREATE TABLE Intake (\n", - " SeqN INTEGER NOT NULL,\n", - " DayCode TINYINT NOT NULL,\n", - " DraBF TINYINT,\n", - " FCID_Code VARCHAR(10),\n", - " Cooked_Status TINYINT,\n", - " Food_Form TINYINT,\n", - " Cooking_Method TINYINT,\n", - " Intake DECIMAL(13,7),\n", - " Intake_BW DECIMAL(13,10),\n", - " PRIMARY KEY(SeqN, DayCode, FCID_Code, Cooked_Status, Food_Form, Cooking_Method)\n", - ") AS SELECT\n", - " SEQN, DAYCODE, DRABF, FCID_Code, Cooked_Status, Food_Form, Cooking_Method, Intake,Intake_BW\n", - "FROM CSVREAD('../../data/food-intake/intake/Commodity_CSFFM_Intake_0510.csv');" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "DROP TABLE IF EXISTS Recipes;\n", - "\n", - "CREATE TABLE Recipes (\n", - " Food_Code VARCHAR(8),\n", - " Mod_Code VARCHAR(8),\n", - " Ingredient_Num TINYINT,\n", - " FCID_Code VARCHAR(10),\n", - " Cooked_Status TINYINT,\n", - " Food_Form TINYINT,\n", - " Cooking_Method TINYINT,\n", - " Commodity_Weight DECIMAL(5, 2),\n", - " CSFII_9498_IND TINYINT,\n", - " WWEIA_9904_IND TINYINT,\n", - " WWEIA_0510_IND TINYINT,\n", - " PRIMARY KEY(Food_Code, Mod_Code, Ingredient_Num)\n", - ") AS SELECT\n", - " Food_Code, Mod_Code, Ingredient_Num, FCID_Code, Cooked_Status, Food_Form, Cooking_Method,\n", - " Commodity_Weight, CSFII_9498_IND, WWEIA_9904_IND, WWEIA_0510_IND\n", - "FROM CSVREAD('../../data/food-intake/recipe/Recipes_WWEIA_FCID_0510.csv');" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "DROP TABLE IF EXISTS FCID_Description;\n", - "\n", - "CREATE TABLE FCID_Description (\n", - " CGN VARCHAR(2),\n", - " CG_Subgroup VARCHAR(6),\n", - " FCID_Code VARCHAR(10),\n", - " FCID_Desc VARCHAR(55),\n", - " PRIMARY KEY (FCID_Code)\n", - ") AS SELECT\n", - " cgn, CG_Subgroup, FCID_Code, FCID_Desc\n", - "FROM CSVREAD('../../data/food-intake/basics/FCID_Code_Description.csv');" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# SELECT Básico" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Todos os Táxis com todos os atributos" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "scrolled": false - }, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "6001453f-3dc3-4176-9911-e50cc345d7d1", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT * FROM Intake LIMIT 10;" - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "c60da0f1-fd2f-448a-abf3-7f5fe4f097c6", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT * FROM Recipes LIMIT 10;" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "656a5f41-3910-405b-908c-95ceacfefac2", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT * FROM Recipes WHERE Food_Code = '27111300' AND Mod_Code = 0;" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "99.79" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "SELECT SUM(Commodity_Weight) FROM Recipes WHERE Food_Code = '27111300' AND Mod_Code = 0;" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "62160" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "SELECT MAX(SeqN) FROM Intake;" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "7154" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "SELECT COUNT(DISTINCT Food_Code) FROM Recipes;" - ] - }, - { - "cell_type": "code", - "execution_count": 62, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "68591d2b-11c0-49ab-9a31-ff130d3679a0", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "DROP VIEW IF EXISTS Participation_Profile;\n", - "\n", - "CREATE VIEW Participation_Profile AS\n", - "SELECT R.FCID_Code, COUNT(DISTINCT (R.Food_Code, R.Mod_Code)) Participation\n", - "FROM Recipes R\n", - "GROUP BY R.FCID_Code;\n", - "\n", - "SELECT * FROM Participation_Profile LIMIT 10;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Marca e Modelo dos Taxis disponíveis" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3499408" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "SELECT COUNT(*) FROM Intake;" - ] - }, - { - "cell_type": "code", - "execution_count": 67, - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "DROP VIEW IF EXISTS Commodity_Profile;\n", - "\n", - "CREATE VIEW Commodity_Profile\n", - "AS SELECT I.FCID_Code, F.FCID_Desc, F.CGN, F.CG_Subgroup, COUNT(DISTINCT I.SeqN) Popularity,\n", - " SUM(I.Intake) Intake_Sum, AVG(I.Intake) Intake_AVG,\n", - " AVG(I.Intake_BW) Intake_BW_AVG, P.Participation Recipes\n", - "FROM Intake I, FCID_Description F, Participation_Profile P\n", - "WHERE I.FCID_Code = F.FCID_Code AND I.FCID_Code = P.FCID_Code\n", - "GROUP BY I.FCID_Code;" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1891731" - ] - }, - "execution_count": 46, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "SELECT SUM(Consumption) FROM Commodity_Profile;" - ] - }, - { - "cell_type": "code", - "execution_count": 68, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "464" - ] - }, - "execution_count": 68, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "CALL CSVWRITE('../../data/food-intake/computed/commodity-profile.csv',\n", - " 'SELECT * FROM Commodity_Profile');" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Taxis fabricados depois do ano 2000" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "59e804da-75e5-4753-9f68-b57bd047a4d7", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT * FROM Taxi WHERE AnoFab > 2000;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Placas que comecem com DK" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "2d632301-5426-471a-aa8f-a0d66c0a7a4d", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT * FROM Taxi WHERE placa LIKE 'DK%';" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Placas com '7' na penultima posicao" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "1488067f-8f2b-4345-881a-4501191ac4cc", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT * FROM Taxi WHERE placa LIKE '%7_';" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Produto Cartesiano" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Produto Cartesiano entre Clientes e Corridas" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "ffbc82b6-7aa3-447d-8979-3c6929083f85", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Cliente.CliId, Cliente.Nome, Corrida.Placa, Corrida.DataPedido\n", - "FROM Cliente, Corrida" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# JOIN Implícito" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Cientes e as respectivas corridas - Join Implícito" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "4199b81b-d08f-4932-b46e-7f63fb5efc13", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Cliente.CliId, Cliente.Nome, Corrida.Placa, Corrida.DataPedido\n", - "FROM Cliente, Corrida\n", - "WHERE Cliente.CliId = Corrida.CliId" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Modelo de taxi para cada corrida" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "da1ad8dd-c0c6-4021-b341-c1eb31b85581", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Corrida.DataPedido, Corrida.Placa, Taxi.Modelo\n", - " FROM Corrida, Taxi\n", - " WHERE Corrida.Placa = Taxi.Placa;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Modelos de Táxi por Cliente (estágio 1)" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "aa0e737b-ecfb-4d29-9ed5-adc857970149", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Cliente.Nome, Corrida.DataPedido, Corrida.Placa, Taxi.Modelo\n", - " FROM Cliente, Corrida, Taxi\n", - " WHERE Cliente.CliId = Corrida.CliId AND Corrida.Placa = Taxi.Placa;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Modelos de Táxi por Cliente (estágio 2)" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "90b817e0-32fd-4d54-ac5c-f93b85572406", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT DISTINCT Cliente.Nome, Taxi.Modelo\n", - " FROM Cliente, Corrida, Taxi\n", - " WHERE Cliente.CliId = Corrida.CliId AND Corrida.Placa = Taxi.Placa;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## ORDER BY" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Nome dos clientes ordenado alfabeticamente" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "db021719-771b-4214-b8da-c89e4b83e905", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Nome\n", - " FROM Cliente\n", - " ORDER BY Nome;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Modelos de taxi e os clientes que os tomaram\n", - "\n", - "Ordena por Modelo, mas não por Cliente:" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "44468c8b-bbbf-4d3f-a8c6-5addcef44280", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT DISTINCT Cl.Nome, T.Modelo\n", - " FROM Cliente Cl, Corrida Co, Taxi T\n", - " WHERE Cl.CliId = Co.CliId AND\n", - " Co.Placa = T.Placa\n", - " ORDER BY T.Modelo;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ordena por Modelo e, para cada Modelo, por Cliente:" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "efe07346-9336-4014-840a-ae621fd52645", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT DISTINCT Cl.Nome, T.Modelo\n", - " FROM Cliente Cl, Corrida Co, Taxi T\n", - " WHERE Cl.CliId = Co.CliId AND\n", - " Co.Placa = T.Placa\n", - " ORDER BY T.Modelo, Cl.Nome;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Cliente e os modelos de taxi tomados\n", - "Ordena por Cliente, mas não por Modelo:" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "6e458511-db72-4ba7-a2d8-1f8c4aa32726", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT DISTINCT Cl.Nome, T.Modelo\n", - " FROM Cliente Cl, Corrida Co, Taxi T\n", - " WHERE Cl.CliId = Co.CliId AND\n", - " Co.Placa = T.Placa\n", - " ORDER BY Cl.Nome;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Ordena por Cliente e, para cada Cliente, por Modelo" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "c2712648-75db-47e2-b0c5-6ca18850ed15", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT DISTINCT Cl.Nome, T.Modelo\n", - " FROM Cliente Cl, Corrida Co, Taxi T\n", - " WHERE Cl.CliId = Co.CliId AND\n", - " Co.Placa = T.Placa\n", - " ORDER BY Cl.Nome, T.Modelo;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## JOIN explícito" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Clientes e respectivas corridas (para clientes que fizeram corrida)" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "13c471a4-2535-4eb6-8c35-f44ef23a48b1", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Cl.CliId, Cl.Nome,\n", - " Co.Placa, Co.DataPedido\n", - " FROM Cliente Cl JOIN Corrida Co\n", - " ON Cl.CliId = Co.CliId;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Taxis e respectivas corridas (para taxis que fizeram corrida)" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "d85dbc38-70dc-4b86-b5bb-70476ff8facb", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Tx.placa, Co.cliid\n", - " FROM Taxi Tx JOIN Corrida Co\n", - " ON Tx.placa = Co.placa;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## NATURAL JOIN" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "d780514b-c004-4d90-98cc-31a797a93186", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Tx.placa, Co.cliid\n", - " FROM Taxi Tx\n", - " NATURAL JOIN Corrida Co;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## LEFT JOIN\n", - "\n", - "### Clientes e respectivas corridas (para todos os clientes)" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "315b218f-e111-45f5-befc-16bfbeb121ea", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Cl.CliId, Cl.Nome,\n", - " Co.Placa, Co.DataPedido\n", - " FROM Cliente Cl LEFT JOIN Corrida Co\n", - " ON Cl.CliId = Co.CliId;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Taxis e respectivas corridas (para todos os taxis)" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "f13f511d-1046-435f-a0a0-19ace68bb1e7", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Tx.placa, Co.cliid\n", - " FROM Taxi Tx LEFT JOIN Corrida Co\n", - " ON Tx.placa = Co.placa;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## RIGHT JOIN\n", - "\n", - "### Corridas e respectivos clientes (para todos os clientes)" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "1fd651c2-95b2-450a-b263-6725d0a65cb9", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Co.placa, Cl.nome\n", - " FROM Corrida Co RIGHT JOIN Cliente Cl\n", - " ON Co.cliid = Cl.cliid;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# GROUP BY" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Modelos de Táxi cadastrados" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "edae2c6e-f7fe-40c0-a78f-d654e0198163", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT T.Modelo\n", - " FROM Taxi T\n", - " GROUP BY T.Modelo;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Total de Táxis por Modelo\n", - "\n", - "Modelos de Táxi cadastrados e quantos táxis há em cada modelo:" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "214902d4-26ad-4de5-8e32-21ef2a583a61", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT T.Modelo, COUNT(*)\n", - " FROM Taxi T\n", - " GROUP BY T.Modelo;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Agregando em mais de um nível" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Modelos de taxi tomados por cada cliente (agrupando por Cliente e por Modelo)" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "04eed808-0f4e-41cd-b623-314b756d0ff2", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Cl.Nome, T.Modelo\n", - " FROM Cliente Cl, Corrida Co, Taxi T\n", - " WHERE Cl.CliId = Co.CliId AND\n", - " Co.Placa = T.Placa;" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "4d6585f7-eb45-4273-b3e6-4bf973ccd6e9", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT DISTINCT Cl.Nome, T.Modelo\n", - " FROM Cliente Cl, Corrida Co, Taxi T\n", - " WHERE Cl.CliId = Co.CliId AND\n", - " Co.Placa = T.Placa;" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "f1817a12-84f8-4d0b-bb72-a4d2804046f7", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Cl.Nome, T.Modelo\n", - " FROM Cliente Cl, Corrida Co, Taxi T\n", - " WHERE Cl.CliId = Co.CliId AND\n", - " Co.Placa = T.Placa\n", - " GROUP BY Cl.Nome, T.Modelo;" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "afc5feab-35e3-485e-80e6-8be1f5562b0f", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Cl.Nome, T.Modelo, COUNT(*)\n", - " FROM Cliente Cl, Corrida Co, Taxi T \n", - " WHERE Cl.CliId = Co.CliId AND \n", - " Co.Placa = T.Placa\n", - " GROUP BY Cl.Nome, T.Modelo;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Funções de agregação sem agrupamento\n", - "\n", - "### Média de ano de fabricação para todos os Táxis\n", - "\n", - "Quando é usada função de agregação sem especificação de agrupamento, toda a tabela é agregada como se fosse um único grupo." - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "4021678b-a559-49a0-83b8-85950fd71a52", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Modelo, AnoFab FROM Taxi;" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "c194a1f5-49f5-4c89-a8d6-ae596bed5760", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Modelo, MIN(AnoFab), MAX(AnoFab), AVG(AnoFab)\n", - " FROM Taxi\n", - " GROUP BY Modelo;" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "2002" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "-- Maior ano de fabricação de toda a tabela\n", - "SELECT MAX(AnoFab) FROM Taxi;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Campos mostrados devem corresponder a agregação\n", - "\n", - "Exemplo sem agregação:" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "ba55be36-e61e-484c-9b35-c822834c29fb", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Modelo, AnoFab\n", - " FROM Taxi;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Modelo e Ano de Fabricação\n", - "\n", - "Este exemplo com agregação produzirá um erro pois `AnoFab` (ano de fabricação) não foi agregado, portanto, pode haver mais de um ano de fabricação para o mesmo modelo." - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": {}, - "outputs": [ - { - "ename": "org.h2.jdbc.JdbcSQLException", - "evalue": " Column \"ANOFAB\" must be in the GROUP BY list; SQL statement", - "output_type": "error", - "traceback": [ - "\u001b[1;31morg.h2.jdbc.JdbcSQLException: Column \"ANOFAB\" must be in the GROUP BY list; SQL statement:\u001b[0;0m", - "\u001b[1;31mSELECT Modelo, AnoFab\u001b[0;0m", - "\u001b[1;31mFROM Taxi\u001b[0;0m", - "\u001b[1;31mGROUP BY Modelo [90016-193]\u001b[0;0m" - ] - } - ], - "source": [ - "SELECT Modelo, AnoFab\n", - " FROM Taxi\n", - " GROUP BY Modelo;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Modelo e Maior Ano de Fabricação\n", - "#### Corrigindo com função de agregação\n", - "\n", - "Este exemplo escolhe o maior ano para cada Modelo agregado." - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "e9b1d513-1d86-4295-8a72-3f9c86eed6a6", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Modelo, MAX(AnoFab)\n", - " FROM Taxi\n", - " GROUP BY Modelo;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# HAVING" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Número de Táxis por Modelo somente para os Táxis com ano de fabricação abaixo de 2000\n", - "\n", - "Exemplo com `WHERE` em que a condição é aplicada antes de se agregar:" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "3755fd11-c608-4230-9d67-bb50be2bcd5f", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "-- Todos os Táxis\n", - "SELECT Modelo, AnoFab\n", - " FROM Taxi;" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "c38896be-3388-47a9-a22e-68cdca42bbbc", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Modelo, AnoFab\n", - " FROM Taxi\n", - " WHERE AnoFab < 2000;" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "8edab26a-9f2d-460f-be88-c52fde5fdad6", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "-- Selecionados e Agregados\n", - "SELECT Modelo, COUNT(*) NUM\n", - " FROM Taxi\n", - " WHERE AnoFab < 2000\n", - " GROUP BY Modelo;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Modelos de Táxi que têm mais de um Táxi do respectivo modelo\n", - "\n", - "Exemplo com `HAVING` em que a condição é aplicada após se agregar:" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "ec5a9c5c-bb27-4f39-b08d-58eaa6489194", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "-- Agrupamento\n", - "SELECT Modelo, COUNT(*) NUM\n", - " FROM Taxi\n", - " GROUP BY Modelo;" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "64608e1e-f8b0-46ee-acc7-2a0a6fb06be5", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "-- Agrupamento com HAVING\n", - "SELECT Modelo, COUNT(*) NUM\n", - " FROM Taxi\n", - " GROUP BY Modelo\n", - " HAVING NUM > 1;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## `HAVING` apenas para campos agregados \n", - "\n", - "### Modelos daqueles Táxis com ano de fabricação acima de 2000\n", - "\n", - "A seleção a seguir produzirá um erro pois está se aplicando uma condição `HAVING` para um campo não agregado. Deveria ter sido usado `WHERE` como foi feito anteriormente." - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": {}, - "outputs": [ - { - "ename": "org.h2.jdbc.JdbcSQLException", - "evalue": " Column \"ANOFAB\" must be in the GROUP BY list; SQL statement", - "output_type": "error", - "traceback": [ - "\u001b[1;31morg.h2.jdbc.JdbcSQLException: Column \"ANOFAB\" must be in the GROUP BY list; SQL statement:\u001b[0;0m", - "\u001b[1;31mSELECT Modelo, COUNT(*) NUM\u001b[0;0m", - "\u001b[1;31mFROM Taxi\u001b[0;0m", - "\u001b[1;31mGROUP BY Modelo\u001b[0;0m", - "\u001b[1;31mHAVING AnoFab > 2000 [90016-193]\u001b[0;0m" - ] - } - ], - "source": [ - "SELECT Modelo, COUNT(*) NUM\n", - " FROM Taxi\n", - " GROUP BY Modelo\n", - " HAVING AnoFab > 2000;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Exemplo de `HAVING` com funções de agregação\n", - "\n", - "### Modelos de Táxi cujo menor Ano de Fabricação seja após 2000" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "37371f14-eee5-4574-b883-5ccc64b7834c", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "-- sem agregacao (nao eh possível resolver a questao)\n", - "SELECT Modelo, AnoFab\n", - " FROM Taxi\n", - " WHERE AnoFab < 2000;" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "93f52f16-b45c-4507-b507-eeda15056c4f", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "-- testando após a agregação\n", - "SELECT Modelo, MAX(AnoFab) MaiorAno\n", - " FROM Taxi\n", - " GROUP BY Modelo\n", - " HAVING MaiorAno < 2000;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# VIEW\n", - "\n", - "### Total de Táxis por Modelo\n", - "\n", - "Agrupamento sem o uso de `VIEW`:" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "ba920327-911c-47cf-ad07-36a7e9cb8927", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "SELECT Modelo, COUNT(*) Numero_Taxis\n", - " FROM taxi\n", - " GROUP BY Modelo;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Tabela de Total de Táxis por Modelo\n", - "\n", - "Transformando o agrupamento na tabela `Contagem_Modelo` com o `VIEW`:" - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "98561721-ba65-42de-af11-d99b5d078f40", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "CREATE VIEW Contagem_Modelo AS\n", - "SELECT Modelo, COUNT(*) Numero_Taxis\n", - " FROM taxi\n", - " GROUP BY Modelo;\n", - "\n", - "SELECT * FROM Contagem_Modelo;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Usando a tabela criada com a `VIEW`:" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 48, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "SELECT MAX(Numero_Taxis) FROM Contagem_Modelo;" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## VIEW se auto-atualiza\n", - "\n", - "A view é um recorte dinâmico, ela se auto-atualiza quando a tabela original é modificada." - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "1d67a773-cb25-4f6b-b232-8422c5e63ec1", - "version_major": 2, - "version_minor": 0 - }, - "method": "display_data" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "INSERT INTO Taxi VALUES ('KMN3412', 'Chevrolet', 'Corsa', 2001, 'QJ572345');\n", - "SELECT * FROM Contagem_Modelo;" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "SQL", - "language": "SQL", - "name": "sql" - }, - "language_info": { - "codemirror_mode": "sql", - "file_extension": ".sql", - "mimetype": "", - "name": "SQL", - "nbconverter_exporter": "", - "version": "" - }, - "toc": { - "base_numbering": 1, - "nav_menu": {}, - "number_sections": false, - "sideBar": false, - "skip_h1_title": false, - "title_cell": "Table of Contents", - "title_sidebar": "Contents", - "toc_cell": false, - "toc_position": {}, - "toc_section_display": false, - "toc_window_display": false - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/sql/food-intake/notebook.tex b/sql/food-intake/notebook.tex deleted file mode 100644 index 7f66b2d..0000000 --- a/sql/food-intake/notebook.tex +++ /dev/null @@ -1,497 +0,0 @@ - -% Default to the notebook output style - - - - -% Inherit from the specified cell style. - - - - - -\documentclass[11pt]{article} - - - - \usepackage[T1]{fontenc} - % Nicer default font (+ math font) than Computer Modern for most use cases - \usepackage{mathpazo} - - % Basic figure setup, for now with no caption control since it's done - % automatically by Pandoc (which extracts ![](path) syntax from Markdown). - \usepackage{graphicx} - % We will generate all images so they have a width \maxwidth. This means - % that they will get their normal width if they fit onto the page, but - % are scaled down if they would overflow the margins. - \makeatletter - \def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth - \else\Gin@nat@width\fi} - \makeatother - \let\Oldincludegraphics\includegraphics - % Set max figure width to be 80% of text width, for now hardcoded. - \renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=.8\maxwidth]{#1}} - % Ensure that by default, figures have no caption (until we provide a - % proper Figure object with a Caption API and a way to capture that - % in the conversion process - todo). - \usepackage{caption} - \DeclareCaptionLabelFormat{nolabel}{} - \captionsetup{labelformat=nolabel} - - \usepackage{adjustbox} % Used to constrain images to a maximum size - \usepackage{xcolor} % Allow colors to be defined - \usepackage{enumerate} % Needed for markdown enumerations to work - \usepackage{geometry} % Used to adjust the document margins - \usepackage{amsmath} % Equations - \usepackage{amssymb} % Equations - \usepackage{textcomp} % defines textquotesingle - % Hack from http://tex.stackexchange.com/a/47451/13684: - \AtBeginDocument{% - \def\PYZsq{\textquotesingle}% Upright quotes in Pygmentized code - } - \usepackage{upquote} % Upright quotes for verbatim code - \usepackage{eurosym} % defines \euro - \usepackage[mathletters]{ucs} % Extended unicode (utf-8) support - \usepackage[utf8x]{inputenc} % Allow utf-8 characters in the tex document - \usepackage{fancyvrb} % verbatim replacement that allows latex - \usepackage{grffile} % extends the file name processing of package graphics - % to support a larger range - % The hyperref package gives us a pdf with properly built - % internal navigation ('pdf bookmarks' for the table of contents, - % internal cross-reference links, web links for URLs, etc.) - \usepackage{hyperref} - \usepackage{longtable} % longtable support required by pandoc >1.10 - \usepackage{booktabs} % table support for pandoc > 1.12.2 - \usepackage[inline]{enumitem} % IRkernel/repr support (it uses the enumerate* environment) - \usepackage[normalem]{ulem} % ulem is needed to support strikethroughs (\sout) - % normalem makes italics be italics, not underlines - - - - - % Colors for the hyperref package - \definecolor{urlcolor}{rgb}{0,.145,.698} - \definecolor{linkcolor}{rgb}{.71,0.21,0.01} - \definecolor{citecolor}{rgb}{.12,.54,.11} - - % ANSI colors - \definecolor{ansi-black}{HTML}{3E424D} - \definecolor{ansi-black-intense}{HTML}{282C36} - \definecolor{ansi-red}{HTML}{E75C58} - \definecolor{ansi-red-intense}{HTML}{B22B31} - \definecolor{ansi-green}{HTML}{00A250} - \definecolor{ansi-green-intense}{HTML}{007427} - \definecolor{ansi-yellow}{HTML}{DDB62B} - \definecolor{ansi-yellow-intense}{HTML}{B27D12} - \definecolor{ansi-blue}{HTML}{208FFB} - \definecolor{ansi-blue-intense}{HTML}{0065CA} - \definecolor{ansi-magenta}{HTML}{D160C4} - \definecolor{ansi-magenta-intense}{HTML}{A03196} - \definecolor{ansi-cyan}{HTML}{60C6C8} - \definecolor{ansi-cyan-intense}{HTML}{258F8F} - \definecolor{ansi-white}{HTML}{C5C1B4} - \definecolor{ansi-white-intense}{HTML}{A1A6B2} - - % commands and environments needed by pandoc snippets - % extracted from the output of `pandoc -s` - \providecommand{\tightlist}{% - \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}} - % Add ',fontsize=\small' for more characters per line - \newenvironment{Shaded}{}{} - \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}} - \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}} - \newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}} - \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}} - \newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}} - \newcommand{\RegionMarkerTok}[1]{{#1}} - \newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}} - \newcommand{\NormalTok}[1]{{#1}} - - % Additional commands for more recent versions of Pandoc - \newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.53,0.00,0.00}{{#1}}} - \newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}} - \newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.73,0.40,0.53}{{#1}}} - \newcommand{\ImportTok}[1]{{#1}} - \newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.73,0.13,0.13}{\textit{{#1}}}} - \newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}} - \newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}} - \newcommand{\VariableTok}[1]{\textcolor[rgb]{0.10,0.09,0.49}{{#1}}} - \newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}} - \newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.40,0.40,0.40}{{#1}}} - \newcommand{\BuiltInTok}[1]{{#1}} - \newcommand{\ExtensionTok}[1]{{#1}} - \newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.74,0.48,0.00}{{#1}}} - \newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.49,0.56,0.16}{{#1}}} - \newcommand{\InformationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}} - \newcommand{\WarningTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}} - - - % Define a nice break command that doesn't care if a line doesn't already - % exist. - \def\br{\hspace*{\fill} \\* } - % Math Jax compatability definitions - \def\gt{>} - \def\lt{<} - % Document parameters - \title{food-intake-analysis-results} - - - - - % Pygments definitions - -\makeatletter -\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax% - \let\PY@ul=\relax \let\PY@tc=\relax% - \let\PY@bc=\relax \let\PY@ff=\relax} -\def\PY@tok#1{\csname PY@tok@#1\endcsname} -\def\PY@toks#1+{\ifx\relax#1\empty\else% - \PY@tok{#1}\expandafter\PY@toks\fi} -\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{% - \PY@it{\PY@bf{\PY@ff{#1}}}}}}} -\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}} - -\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}} -\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}} -\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}} -\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}} -\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}} -\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}} -\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} -\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}} -\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} -\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}} -\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}} -\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit} -\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf} -\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} -\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}} -\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}} -\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}} -\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} -\expandafter\def\csname PY@tok@fm\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} -\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@vm\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} -\expandafter\def\csname PY@tok@sa\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@dl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} -\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} -\expandafter\def\csname PY@tok@ch\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@cpf\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} -\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} - -\def\PYZbs{\char`\\} -\def\PYZus{\char`\_} -\def\PYZob{\char`\{} -\def\PYZcb{\char`\}} -\def\PYZca{\char`\^} -\def\PYZam{\char`\&} -\def\PYZlt{\char`\<} -\def\PYZgt{\char`\>} -\def\PYZsh{\char`\#} -\def\PYZpc{\char`\%} -\def\PYZdl{\char`\$} -\def\PYZhy{\char`\-} -\def\PYZsq{\char`\'} -\def\PYZdq{\char`\"} -\def\PYZti{\char`\~} -% for compatibility with earlier versions -\def\PYZat{@} -\def\PYZlb{[} -\def\PYZrb{]} -\makeatother - - - % Exact colors from NB - \definecolor{incolor}{rgb}{0.0, 0.0, 0.5} - \definecolor{outcolor}{rgb}{0.545, 0.0, 0.0} - - - - - % Prevent overflowing lines due to hard-to-break entities - \sloppy - % Setup hyperref package - \hypersetup{ - breaklinks=true, % so long urls are correctly broken across lines - colorlinks=true, - urlcolor=urlcolor, - linkcolor=linkcolor, - citecolor=citecolor, - } - % Slightly bigger margins than the latex defaults - - \geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in} - - - - \begin{document} - - - \maketitle - - - - - \hypertarget{u.s.-epa-food-commodity-intake-database-fcid}{% -\section{U.S. EPA Food Commodity Intake Database -(FCID)}\label{u.s.-epa-food-commodity-intake-database-fcid}} - -\hypertarget{httpsfcid.foodrisk.org}{% -\subsection{\texorpdfstring{\url{https://fcid.foodrisk.org/}}{https://fcid.foodrisk.org/}}\label{httpsfcid.foodrisk.org}} - - Ativando uma conexão de banco de dados em memória usando o SGBD H2: - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}1}]:} \PY{o}{\PYZpc{}}\PY{n}{defaultDatasource} \PY{n}{jdbc}\PY{p}{:}\PY{n}{h2}\PY{p}{:}\PY{n}{mem}\PY{p}{:}\PY{n}{db} -\end{Verbatim} - - - \hypertarget{importando-tabelas-do-fcid}{% -\section{Importando Tabelas do FCID}\label{importando-tabelas-do-fcid}} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}2}]:} \PY{k}{DROP} \PY{k}{TABLE} \PY{k}{IF} \PY{k}{EXISTS} \PY{n}{Crop\PYZus{}Group}\PY{p}{;} - \PY{k}{DROP} \PY{k}{TABLE} \PY{k}{IF} \PY{k}{EXISTS} \PY{n}{FCID\PYZus{}Description}\PY{p}{;} - \PY{k}{DROP} \PY{k}{TABLE} \PY{k}{IF} \PY{k}{EXISTS} \PY{n}{Recipes}\PY{p}{;} - \PY{k}{DROP} \PY{k}{TABLE} \PY{k}{IF} \PY{k}{EXISTS} \PY{n}{Intake}\PY{p}{;} - - \PY{k}{CREATE} \PY{k}{TABLE} \PY{n}{Crop\PYZus{}Group} \PY{p}{(} - \PY{n}{CGN} \PY{n+nb}{VARCHAR}\PY{p}{(}\PY{l+m+mi}{2}\PY{p}{)}\PY{p}{,} - \PY{n}{CGL} \PY{n+nb}{VARCHAR}\PY{p}{(}\PY{l+m+mi}{6}\PY{p}{)}\PY{p}{,} - \PY{n}{Crop\PYZus{}Group\PYZus{}Description} \PY{n+nb}{VARCHAR}\PY{p}{(}\PY{l+m+mi}{80}\PY{p}{)}\PY{p}{,} - \PY{k}{PRIMARY} \PY{k}{KEY} \PY{p}{(}\PY{n}{CGL}\PY{p}{)} - \PY{p}{)} \PY{k}{AS} \PY{k}{SELECT} - \PY{n}{CGN}\PY{p}{,} \PY{n}{CGL}\PY{p}{,} \PY{n}{Crop\PYZus{}Group\PYZus{}Description} - \PY{k}{FROM} \PY{n}{CSVREAD}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}../../data/food\PYZhy{}intake/basics/FCID\PYZus{}Cropgroup\PYZus{}Description.csv\PYZsq{}}\PY{p}{)}\PY{p}{;} - - \PY{k}{CREATE} \PY{k}{TABLE} \PY{n}{FCID\PYZus{}Description} \PY{p}{(} - \PY{n}{CGN} \PY{n+nb}{VARCHAR}\PY{p}{(}\PY{l+m+mi}{2}\PY{p}{)}\PY{p}{,} - \PY{n}{CG\PYZus{}Subgroup} \PY{n+nb}{VARCHAR}\PY{p}{(}\PY{l+m+mi}{6}\PY{p}{)}\PY{p}{,} - \PY{n}{FCID\PYZus{}Code} \PY{n+nb}{VARCHAR}\PY{p}{(}\PY{l+m+mi}{10}\PY{p}{)}\PY{p}{,} - \PY{n}{FCID\PYZus{}Desc} \PY{n+nb}{VARCHAR}\PY{p}{(}\PY{l+m+mi}{55}\PY{p}{)}\PY{p}{,} - \PY{k}{PRIMARY} \PY{k}{KEY} \PY{p}{(}\PY{n}{FCID\PYZus{}Code}\PY{p}{)}\PY{p}{,} - \PY{p}{)} \PY{k}{AS} \PY{k}{SELECT} - \PY{n}{cgn}\PY{p}{,} \PY{n}{CG\PYZus{}Subgroup}\PY{p}{,} \PY{n}{FCID\PYZus{}Code}\PY{p}{,} \PY{n}{FCID\PYZus{}Desc} - \PY{k}{FROM} \PY{n}{CSVREAD}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}../../data/food\PYZhy{}intake/basics/FCID\PYZus{}Code\PYZus{}Description.csv\PYZsq{}}\PY{p}{)}\PY{p}{;} - - \PY{k}{CREATE} \PY{k}{TABLE} \PY{n}{Recipes} \PY{p}{(} - \PY{n}{Food\PYZus{}Code} \PY{n+nb}{VARCHAR}\PY{p}{(}\PY{l+m+mi}{8}\PY{p}{)}\PY{p}{,} - \PY{n}{Mod\PYZus{}Code} \PY{n+nb}{VARCHAR}\PY{p}{(}\PY{l+m+mi}{8}\PY{p}{)}\PY{p}{,} - \PY{n}{Ingredient\PYZus{}Num} \PY{n}{TINYINT}\PY{p}{,} - \PY{n}{FCID\PYZus{}Code} \PY{n+nb}{VARCHAR}\PY{p}{(}\PY{l+m+mi}{10}\PY{p}{)}\PY{p}{,} - \PY{n}{Cooked\PYZus{}Status} \PY{n}{TINYINT}\PY{p}{,} - \PY{n}{Food\PYZus{}Form} \PY{n}{TINYINT}\PY{p}{,} - \PY{n}{Cooking\PYZus{}Method} \PY{n}{TINYINT}\PY{p}{,} - \PY{n}{Commodity\PYZus{}Weight} \PY{n+nb}{DECIMAL}\PY{p}{(}\PY{l+m+mi}{5}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)}\PY{p}{,} - \PY{n}{CSFII\PYZus{}9498\PYZus{}IND} \PY{n}{TINYINT}\PY{p}{,} - \PY{n}{WWEIA\PYZus{}9904\PYZus{}IND} \PY{n}{TINYINT}\PY{p}{,} - \PY{n}{WWEIA\PYZus{}0510\PYZus{}IND} \PY{n}{TINYINT}\PY{p}{,} - \PY{k}{PRIMARY} \PY{k}{KEY}\PY{p}{(}\PY{n}{Food\PYZus{}Code}\PY{p}{,} \PY{n}{Mod\PYZus{}Code}\PY{p}{,} \PY{n}{Ingredient\PYZus{}Num}\PY{p}{)}\PY{p}{,} - \PY{k}{FOREIGN} \PY{k}{KEY}\PY{p}{(}\PY{n}{FCID\PYZus{}Code}\PY{p}{)} - \PY{k}{REFERENCES} \PY{n}{FCID\PYZus{}Description}\PY{p}{(}\PY{n}{FCID\PYZus{}Code}\PY{p}{)} - \PY{k}{ON} \PY{k}{DELETE} \PY{k}{NO} \PY{n}{ACTION} - \PY{k}{ON} \PY{k}{UPDATE} \PY{k}{NO} \PY{n}{ACTION} - \PY{p}{)} \PY{k}{AS} \PY{k}{SELECT} - \PY{n}{Food\PYZus{}Code}\PY{p}{,} \PY{n}{Mod\PYZus{}Code}\PY{p}{,} \PY{n}{Ingredient\PYZus{}Num}\PY{p}{,} \PY{n}{FCID\PYZus{}Code}\PY{p}{,} \PY{n}{Cooked\PYZus{}Status}\PY{p}{,} \PY{n}{Food\PYZus{}Form}\PY{p}{,} \PY{n}{Cooking\PYZus{}Method}\PY{p}{,} - \PY{n}{Commodity\PYZus{}Weight}\PY{p}{,} \PY{n}{CSFII\PYZus{}9498\PYZus{}IND}\PY{p}{,} \PY{n}{WWEIA\PYZus{}9904\PYZus{}IND}\PY{p}{,} \PY{n}{WWEIA\PYZus{}0510\PYZus{}IND} - \PY{k}{FROM} \PY{n}{CSVREAD}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}../../data/food\PYZhy{}intake/recipes/Recipes\PYZus{}WWEIA\PYZus{}FCID\PYZus{}0510.csv\PYZsq{}}\PY{p}{)}\PY{p}{;} - - \PY{k}{CREATE} \PY{k}{TABLE} \PY{n}{Intake} \PY{p}{(} - \PY{n}{SeqN} \PY{n+nb}{INTEGER} \PY{k}{NOT} \PY{k}{NULL}\PY{p}{,} - \PY{n}{DayCode} \PY{n}{TINYINT} \PY{k}{NOT} \PY{k}{NULL}\PY{p}{,} - \PY{n}{DraBF} \PY{n}{TINYINT}\PY{p}{,} - \PY{n}{FCID\PYZus{}Code} \PY{n+nb}{VARCHAR}\PY{p}{(}\PY{l+m+mi}{10}\PY{p}{)}\PY{p}{,} - \PY{n}{Cooked\PYZus{}Status} \PY{n}{TINYINT}\PY{p}{,} - \PY{n}{Food\PYZus{}Form} \PY{n}{TINYINT}\PY{p}{,} - \PY{n}{Cooking\PYZus{}Method} \PY{n}{TINYINT}\PY{p}{,} - \PY{n}{Intake} \PY{n+nb}{DECIMAL}\PY{p}{(}\PY{l+m+mi}{13}\PY{p}{,}\PY{l+m+mi}{7}\PY{p}{)}\PY{p}{,} - \PY{n}{Intake\PYZus{}BW} \PY{n+nb}{DECIMAL}\PY{p}{(}\PY{l+m+mi}{13}\PY{p}{,}\PY{l+m+mi}{10}\PY{p}{)}\PY{p}{,} - \PY{k}{PRIMARY} \PY{k}{KEY}\PY{p}{(}\PY{n}{SeqN}\PY{p}{,} \PY{n}{DayCode}\PY{p}{,} \PY{n}{FCID\PYZus{}Code}\PY{p}{,} \PY{n}{Cooked\PYZus{}Status}\PY{p}{,} \PY{n}{Food\PYZus{}Form}\PY{p}{,} \PY{n}{Cooking\PYZus{}Method}\PY{p}{)}\PY{p}{,} - \PY{k}{FOREIGN} \PY{k}{KEY}\PY{p}{(}\PY{n}{FCID\PYZus{}Code}\PY{p}{)} - \PY{k}{REFERENCES} \PY{n}{FCID\PYZus{}Description}\PY{p}{(}\PY{n}{FCID\PYZus{}Code}\PY{p}{)} - \PY{k}{ON} \PY{k}{DELETE} \PY{k}{NO} \PY{n}{ACTION} - \PY{k}{ON} \PY{k}{UPDATE} \PY{k}{NO} \PY{n}{ACTION} - \PY{p}{)} \PY{k}{AS} \PY{k}{SELECT} - \PY{n}{SEQN}\PY{p}{,} \PY{n}{DAYCODE}\PY{p}{,} \PY{n}{DRABF}\PY{p}{,} \PY{n}{FCID\PYZus{}Code}\PY{p}{,} \PY{n}{Cooked\PYZus{}Status}\PY{p}{,} \PY{n}{Food\PYZus{}Form}\PY{p}{,} \PY{n}{Cooking\PYZus{}Method}\PY{p}{,} \PY{n}{Intake}\PY{p}{,}\PY{n}{Intake\PYZus{}BW} - \PY{k}{FROM} \PY{n}{CSVREAD}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}../../data/food\PYZhy{}intake/consumption/Commodity\PYZus{}CSFFM\PYZus{}Intake\PYZus{}0510\PYZhy{}cropped.csv\PYZsq{}}\PY{p}{)}\PY{p}{;} -\end{Verbatim} - - - \hypertarget{visualizando-as-tabelas}{% -\section{Visualizando as Tabelas}\label{visualizando-as-tabelas}} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}3}]:} \PY{k}{SELECT} \PY{o}{*} \PY{k}{FROM} \PY{n}{FCID\PYZus{}Description} \PY{k}{LIMIT} \PY{l+m+mi}{10}\PY{p}{;} -\end{Verbatim} - - - - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}4}]:} \PY{k}{SELECT} \PY{o}{*} \PY{k}{FROM} \PY{n}{Recipes} \PY{k}{LIMIT} \PY{l+m+mi}{10}\PY{p}{;} -\end{Verbatim} - - - - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}5}]:} \PY{k}{SELECT} \PY{o}{*} \PY{k}{FROM} \PY{n}{Intake} \PY{k}{LIMIT} \PY{l+m+mi}{10}\PY{p}{;} -\end{Verbatim} - - - - - \hypertarget{apresentando-uma-receita}{% -\subsection{1) Apresentando uma -Receita}\label{apresentando-uma-receita}} - -\begin{itemize} -\tightlist -\item - Liste os ingredientes da receita de código \texttt{27111300} - Mexican - style beef stew, no potatoes, tomato-based sauce (mixture). -\item - Não devem aparecer as modificações da receita. -\item - Mostre apenas o código da receita, o código de cada ingrediente, sua - ordem e a participação no peso. -\end{itemize} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}6}]:} \PY{k}{SELECT} \PY{n}{Food\PYZus{}Code}\PY{p}{,} \PY{n}{FCID\PYZus{}Code}\PY{p}{,} \PY{n}{Ingredient\PYZus{}Num}\PY{p}{,} \PY{n}{Commodity\PYZus{}Weight} - \PY{k}{FROM} \PY{n}{Recipes} - \PY{k}{WHERE} \PY{n}{Food\PYZus{}Code} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}27111300\PYZsq{}} \PY{k}{AND} \PY{n}{Mod\PYZus{}Code} \PY{o}{=} \PY{l+m+mi}{0}\PY{p}{;} -\end{Verbatim} - - - - - \hypertarget{receita-com-nomes-de-ingredientes}{% -\subsection{2) Receita com Nomes de -Ingredientes}\label{receita-com-nomes-de-ingredientes}} - -\begin{itemize} -\tightlist -\item - Aprimore a solução (1) para apresentar o nome dos ingredientes junto - com seus códigos. -\end{itemize} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}7}]:} \PY{k}{SELECT} \PY{n}{R}\PY{p}{.}\PY{n}{Food\PYZus{}Code}\PY{p}{,} \PY{n}{R}\PY{p}{.}\PY{n}{FCID\PYZus{}Code}\PY{p}{,} \PY{n}{F}\PY{p}{.}\PY{n}{FCID\PYZus{}Desc}\PY{p}{,} \PY{n}{R}\PY{p}{.}\PY{n}{Ingredient\PYZus{}Num}\PY{p}{,} \PY{n}{R}\PY{p}{.}\PY{n}{Commodity\PYZus{}Weight} - \PY{k}{FROM} \PY{n}{Recipes} \PY{n}{R}\PY{p}{,} \PY{n}{FCID\PYZus{}Description} \PY{n}{F} - \PY{k}{WHERE} \PY{n}{R}\PY{p}{.}\PY{n}{FCID\PYZus{}Code} \PY{o}{=} \PY{n}{F}\PY{p}{.}\PY{n}{FCID\PYZus{}Code} \PY{k}{AND} \PY{n}{R}\PY{p}{.}\PY{n}{Food\PYZus{}Code} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}27111300\PYZsq{}} \PY{k}{AND} \PY{n}{R}\PY{p}{.}\PY{n}{Mod\PYZus{}Code} \PY{o}{=} \PY{l+m+mi}{0}\PY{p}{;} -\end{Verbatim} - - - - - \hypertarget{grupos-alimentares-da-receita}{% -\subsection{3) Grupos Alimentares da -Receita}\label{grupos-alimentares-da-receita}} - -\begin{itemize} -\tightlist -\item - A partir da receita escolhida em (1), apresente os grupos alimentares - dos ingredientes contidos na receita. -\item - Cada grupo alimentar só deve aparecer uma vez no resultado. -\item - Para se obter o nome do grupo alimentar na tabela \texttt{Crop\_Group} - (não o subgrupo) devem ser considerados os registros em que a coluna - \texttt{CGN} é igual à \texttt{CGL}. -\end{itemize} - - \begin{Verbatim}[commandchars=\\\{\}] -{\color{incolor}In [{\color{incolor}8}]:} \PY{k}{SELECT} \PY{k}{DISTINCT} \PY{k}{C}\PY{p}{.}\PY{n}{Crop\PYZus{}Group\PYZus{}Description} - \PY{k}{FROM} \PY{n}{Recipes} \PY{n}{R}\PY{p}{,} \PY{n}{FCID\PYZus{}Description} \PY{n}{F}\PY{p}{,} \PY{n}{Crop\PYZus{}Group} \PY{k}{C} - \PY{k}{WHERE} \PY{n}{R}\PY{p}{.}\PY{n}{FCID\PYZus{}Code} \PY{o}{=} \PY{n}{F}\PY{p}{.}\PY{n}{FCID\PYZus{}Code} \PY{k}{AND} \PY{n}{F}\PY{p}{.}\PY{n}{CGN} \PY{o}{=} \PY{k}{C}\PY{p}{.}\PY{n}{CGL} \PY{k}{AND} \PY{n}{R}\PY{p}{.}\PY{n}{Food\PYZus{}Code} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}27111300\PYZsq{}} \PY{k}{AND} \PY{n}{R}\PY{p}{.}\PY{n}{Mod\PYZus{}Code} \PY{o}{=} \PY{l+m+mi}{0}\PY{p}{;} -\end{Verbatim} - - - - - \hypertarget{participauxe7uxe3o-nas-receitas}{% -\subsection{4) Participação nas -Receitas}\label{participauxe7uxe3o-nas-receitas}} - -\begin{itemize} -\tightlist -\item - Liste o nome de cada um dos produtos alimentares seguido do número de - receitas em que ele participa. -\item - A lista deve ser apresentada em ordem decrescente de número de - participação em receitas. -\end{itemize} - - \hypertarget{consumo-muxe9dio-de-grupos-de-alimentos}{% -\subsection{5) Consumo Médio de Grupos de -Alimentos}\label{consumo-muxe9dio-de-grupos-de-alimentos}} - -\begin{itemize} -\tightlist -\item - Para cada Grupo de Alimentos (cada CGN diferente na tabela - Crop\_Group), apresente sua descrição e o consumo médio deste grupo de - alimentos. -\item - O consumo é definido pelo campo \texttt{Intake} da tabela - \texttt{Intake}. -\end{itemize} - - - % Add a bibliography block to the postdoc - - - - \end{document}