Last active
October 1, 2015 16:20
-
-
Save jgomo3/81fe21c5e1bd964b8063 to your computer and use it in GitHub Desktop.
Comparación entre dos expresiones SQL, con y sin CTE
This file contains 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
-- Sin CTE | |
select | |
nombre | |
, comp.cantidad_suma as cant_comp | |
, fact.cantidad_suma as cant_fact | |
from | |
productos as prod | |
, (select sum(cantidad) as cantidad_suma, producto from compras) as comp | |
, (select sum(cantidad) as cantidad_suma, producto from facturas) as fact | |
where | |
prod.producto = comp.producto | |
and fact.producto = prod.producto | |
; | |
-- Con CTE | |
with | |
cp as (select sum(cantidad) as cant_sum, producto from compras) | |
, ft as (select sum(cantidad) as cant_sum, producto from facturas) | |
, tot as ( | |
select | |
cp.cant_sum as c_comp | |
, ft.cant_sum as c_fact | |
, ft.producto | |
from | |
cp | |
, ft | |
where | |
cp.producto = ft.producto | |
) | |
select | |
productos.nombre | |
, tot.c_comp | |
, tot.c_fact | |
from | |
productos | |
, tot | |
where | |
tot.producto = productos.product | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment