Created
February 5, 2026 23:36
-
-
Save urielhdz/ade08596b2b5aa1807853266d7ba6801 to your computer and use it in GitHub Desktop.
Query de subconsultas y JOINs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT | |
| MAX(products.name) as nombre_producto, | |
| MAX(labels.name) as marca, | |
| COUNT(order_positions.id) as vendidos, | |
| SUM(order_positions.amount * order_positions.price) as ingreso, | |
| MAX(customers.firstname) cliente_mas_compras | |
| FROM webshop.products products | |
| INNER JOIN webshop.labels labels ON labels.id = products.labelid | |
| INNER JOIN webshop.articles articles ON products.id = articles.productid | |
| INNER JOIN webshop.order_positions order_positions ON order_positions.articleid = articles.id | |
| INNER JOIN ( | |
| SELECT c.id cliente, p2.id producto, COUNT(p2.id) compras | |
| FROM webshop.customer c | |
| INNER JOIN webshop.order o2 ON o2.customer = c.id | |
| INNER JOIN webshop.order_positions o_p2 ON o_p2.orderid = o2.id | |
| INNER JOIN webshop.articles a2 ON o_p2.articleid = a2.id | |
| INNER JOIN webshop.products p2 ON a2.productid = p2.id | |
| GROUP BY p2.id, c.id | |
| HAVING COUNT(p2.id) = ( | |
| SELECT MAX(conteo_iterno) FROM( | |
| SELECT COUNT(o2.id) as conteo_iterno | |
| FROM webshop.order_positions opi | |
| INNER JOIN webshop.order oi ON opi.orderid = oi.id | |
| INNER JOIN webshop.articles ai ON opi.articleid = ai.id | |
| WHERE ai.productid = p2.id | |
| GROUP by oi.customer | |
| ) | |
| ) | |
| ) tabla_maximos ON tabla_maximos.producto = products.id | |
| INNER JOIN webshop.customer customers ON customers.id = tabla_maximos.cliente | |
| GROUP BY products.id | |
| ORDER BY COUNT(order_positions.id) DESC | |
| LIMIT 10; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Buen día. Mis disculpas, profe, tengo una duda. ¿Por qué al ejecutar la siguiente consulta para el Jacket Flavko de la marca Three Gun (producto más vendido según el resultado) obtengo datos completamente distintos para unidades vendidas y el ingreso total? Igualmente con el resto en el top.
SELECT SUM(op.amount) AS vendidos, SUM(op.amount * op.price) AS ingreso FROM webshop.products p INNER JOIN webshop.labels l ON l.id = p.labelid INNER JOIN webshop.articles a ON a.productid = p.id INNER JOIN webshop.order_positions op ON op.articleid = a.id WHERE p.name = 'Jacket Flavko' AND l.name = 'Three Gun';Además al realizar un
SELECT SUM(vendidos) FROM ( -- Query del gist sin el LIMIT );retorna 67.189, ¿no se supone que cuenta números de registros en order_position cuyo máximo no debería superar su total de 5,985 según el esquema?Probablemente solo estoy razonando mal, aun así, agradecería mucho si pudiera comentarnos más sobre su funcionamiento, especialmente la consulta interna.