From 6ae7d8da54c4590cba7b2411dc1d7ee3e67533fe Mon Sep 17 00:00:00 2001 From: "glauciasgi@gmail.com" Date: Sat, 27 May 2023 00:41:20 +0000 Subject: [PATCH] Day 03 Comandos JOIN, UNON ALL e Subqueries --- day03/JOIN.sql | 25 ++++++ day03/PIZZA_SELECT.sql | 183 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 day03/JOIN.sql create mode 100644 day03/PIZZA_SELECT.sql diff --git a/day03/JOIN.sql b/day03/JOIN.sql new file mode 100644 index 0000000..d9104ff --- /dev/null +++ b/day03/JOIN.sql @@ -0,0 +1,25 @@ +-- Databricks notebook source +select +t1.*, +t2.vlPreco +from silver.pizza_query.item_pedido as t1 +left join silver.pizza_query.produto as t2 +on t1.descItem = t2.descItem + +-- COMMAND ---------- + +select +t1.idPedido, +sum(t2.vlPreco) as vl_pedido +from silver.pizza_query.item_pedido as t1 + +left join silver.pizza_query.produto as t2 +on t1.descItem = t2.descItem + +where t1.descTipoItem<> 'bebida' + +GROUP by t1.idPedido + +-- COMMAND ---------- + + diff --git a/day03/PIZZA_SELECT.sql b/day03/PIZZA_SELECT.sql new file mode 100644 index 0000000..33d3d0e --- /dev/null +++ b/day03/PIZZA_SELECT.sql @@ -0,0 +1,183 @@ +-- Databricks notebook source +select +* +from silver.pizza_query.item_pedido + +-- COMMAND ---------- + +SELECT descItem, +count(*) as qtdPedido +FROM silver.pizza_query.item_pedido +WHERE descTipoItem = 'borda' + +GROUP BY descItem +ORDER BY qtdPedido DESC +LIMIT 1 + +-- COMMAND ---------- + +SELECT descItem, +count(*) as qtdPedido +FROM silver.pizza_query.item_pedido +WHERE descTipoItem = 'massa' + +GROUP BY descItem +ORDER BY qtdPedido DESC +LIMIT 1 + +-- COMMAND ---------- + +SELECT descItem, +count(*) as qtdPedido +FROM silver.pizza_query.item_pedido +WHERE descTipoItem LIKE '%ingrediente%' + +GROUP BY descItem +ORDER BY qtdPedido DESC +LIMIT 5 + +-- COMMAND ---------- + +SELECT descItem, +count(*) as qtdPedido +FROM silver.pizza_query.item_pedido +WHERE descTipoItem LIKE '%queijo%' + +GROUP BY descItem +ORDER BY qtdPedido DESC +LIMIT 1 + +-- COMMAND ---------- + +SELECT descItem, +count(*) as qtdPedido +FROM silver.pizza_query.item_pedido +WHERE descTipoItem = 'bebida' + +GROUP BY descItem +ORDER BY qtdPedido DESC +LIMIT 1 + +-- COMMAND ---------- + +--Descobrindo a pizza perfeita + SELECT + * + FROM( + + (SELECT descItem, + count(*) as qtdPedido + FROM silver.pizza_query.item_pedido + WHERE descTipoItem = 'borda' + + GROUP BY descItem + ORDER BY qtdPedido DESC + LIMIT 1) + + UNION ALL + (SELECT descItem, + count(*) as qtdPedido + FROM silver.pizza_query.item_pedido + WHERE descTipoItem = 'massa' + + GROUP BY descItem + ORDER BY qtdPedido DESC + LIMIT 1) + + UNION ALL + (SELECT descItem, + count(*) as qtdPedido + FROM silver.pizza_query.item_pedido + WHERE descTipoItem LIKE '%ingrediente%' + + GROUP BY descItem + ORDER BY qtdPedido DESC + LIMIT 5) + + UNION ALL + (SELECT descItem, + count(*) as qtdPedido + FROM silver.pizza_query.item_pedido + WHERE descTipoItem LIKE '%queijo%' + + GROUP BY descItem + ORDER BY qtdPedido DESC + LIMIT 1) + + UNION ALL + (SELECT descItem, + count(*) as qtdPedido + FROM silver.pizza_query.item_pedido + WHERE descTipoItem = 'bebida' + + GROUP BY descItem + ORDER BY qtdPedido DESC + LIMIT 1) + ) AS t1 + + LEFT JOIN silver.pizza_query.produto AS t2 + ON t1.descItem = t2.descItem + +-- COMMAND ---------- + +--Descobrindo a pizza perfeita + SELECT + sum(vlPreco) + FROM( + + (SELECT descItem, + count(*) as qtdPedido + FROM silver.pizza_query.item_pedido + WHERE descTipoItem = 'borda' + + GROUP BY descItem + ORDER BY qtdPedido DESC + LIMIT 1) + + UNION ALL + (SELECT descItem, + count(*) as qtdPedido + FROM silver.pizza_query.item_pedido + WHERE descTipoItem = 'massa' + + GROUP BY descItem + ORDER BY qtdPedido DESC + LIMIT 1) + + UNION ALL + (SELECT descItem, + count(*) as qtdPedido + FROM silver.pizza_query.item_pedido + WHERE descTipoItem LIKE '%ingrediente%' + + GROUP BY descItem + ORDER BY qtdPedido DESC + LIMIT 5) + + UNION ALL + (SELECT descItem, + count(*) as qtdPedido + FROM silver.pizza_query.item_pedido + WHERE descTipoItem LIKE '%queijo%' + + GROUP BY descItem + ORDER BY qtdPedido DESC + LIMIT 1) + + UNION ALL + (SELECT descItem, + count(*) as qtdPedido + FROM silver.pizza_query.item_pedido + WHERE descTipoItem = 'bebida' + + GROUP BY descItem + ORDER BY qtdPedido DESC + LIMIT 1) + ) AS t1 + + LEFT JOIN silver.pizza_query.produto AS t2 + ON t1.descItem = t2.descItem + +-- COMMAND ---------- + +