Logo Studenta

clase 9

¡Este material tiene más páginas!

Vista previa del material en texto

Pág  1
BASES DE DATOS
BASES DE DATOS
SQL - REUNION
Pág  2
BASES DE DATOS
La posibilidad de efectuar una consulta reuniendo dos o más tablas 
es una de las características más potente de los RDBMS.
El listado de los atributos de cada tablas se especifica por su 
nombre. Si hubiesen atributos con el mismo nombre, se antecede 
el nombre de la tabla.
La condición de reunión puede hacerse en forma:
 Explícita: usando la cláusula JOIN 
 Implícita: especificando la en la condición de la restricción. 
Esto es similar a realizar un producto cartesiano junto a 
una restricción de igualdad. Sólo que se hará de forma 
optima.
REUNION (EQUIREUNION)
Pág  3
BASES DE DATOS
Ventas Clientes
NumVenta NumCliente Fecha Importe NumCliente Nombre
1 1 01/10/07 1000 1 Consumidor final
2 1 03/10/07 1500 2 Alberto
3 2 03/10/07 1250 3 Pedro
4 1 10/10/07 2500
5 2 16/10/07 1720
Relacionar las ventas con el cliente al que se la realizó cada una
SELECT * FROM Ventas INNER JOIN Clientes 
ON Ventas.NumCliente = Clientes.NumCliente
NumVenta Ventas.NumCliente Fecha Importe Clientes.NumCliente Nombre
1 1 01/10/07 1000 1 Consumidor final
2 1 03/10/07 1500 1 Consumidor final
3 2 03/10/07 1250 2 Alberto
4 1 10/10/07 2500 1 Consumidor final
5 2 16/10/07 1720 2 Alberto
REUNION EXPLICITA – INNER JOIN
SELECT * FROM Ventas INNER JOIN Clientes USING (NumCliente)
Pág  4
BASES DE DATOS
Ventas Clientes
NumVenta NumCliente Fecha Importe NumCliente Nombre
1 1 01/10/07 1000 1 Consumidor final
2 1 03/10/07 1500 2 Alberto
3 2 03/10/07 1250 3 Pedro
4 1 10/10/07 2500
5 2 16/10/07 1720
SELECT * FROM Ventas, Clientes 
WHERE Ventas.NumCliente = Clientes.NumCliente
NumVenta Ventas.NumCliente Fecha Importe Clientes.NumCliente Nombre
1 1 01/10/07 1000 1 Consumidor final
2 1 03/10/07 1500 1 Consumidor final
3 2 03/10/07 1250 2 Alberto
4 1 10/10/07 2500 1 Consumidor final
5 2 16/10/07 1720 2 Alberto
REUNION IMPLICITA
Recordar la definición de la operación Equireunión
Pág  5
BASES DE DATOS
Ventas Clientes
NumVenta NumCliente Fecha Importe NumCliente Nombre
1 1 01/10/07 1000 1 Consumidor final
2 1 03/10/07 1500 2 Alberto
3 2 03/10/07 1250 3 Pedro
4 1 10/10/07 2500
5 2 16/10/07 1720
Cantidades totales vendidos por cliente (incluyendo el nombre del cliente).
SELECT C.NumCliente, Nombre, COUNT(*) As Cantidad
FROM Ventas V INNER JOIN Clientes C USING (NumCliente)
GROUP BY C.NumCliente, Nombre
REUNION + GRUPOS
NumCliente Nombre Cantidad
1 Consumidor final 3
2 Alberto 2
Importante
Es necesario agrupar por NumCliente ya que 
si tomamos sólo el nombre podríamos hablar 
de clientes distintos que se llamen igual
Los atributos
Por los cuales agrupamos (están antes de la 
función agregada) deben aparecer todos en 
GROUP BY
Pág  6
BASES DE DATOS
Ventas Clientes
NumVenta NumCliente Fecha Importe NumCliente Nombre
1 1 01/10/07 1000 1 Consumidor final
2 1 03/10/07 1500 2 Alberto
3 2 03/10/07 1250 3 Pedro
4 1 10/10/07 2500
5 2 16/10/07 1720
Importes totales vendidos por cliente (incluyendo el nombre del cliente).
SELECT C.NumCliente, Nombre, SUM(Importe) As Importe
FROM Clientes C INNER JOIN Ventas V USING (NumCliente)
GROUP BY 1, 2
REUNION + GRUPOS
NumCliente Nombre Importe
1 Consumidor final 5000
2 Alberto 2970
Orden de la tablas
Al ser INNER JOIN, es indistinto el orden en 
que se especifiquen las tablas
Pág  7
BASES DE DATOS
Ventas Clientes
NumVenta NumCliente Fecha Importe NumCliente Nombre
1 1 01/10/07 1000 1 Consumidor final
2 1 03/10/07 1500 2 Alberto
3 2 03/10/07 1250 3 Pedro
4 1 10/10/07 2500
5 2 16/10/07 1720
Importes totales vendidos a todos los clientes.
SELECT C.NumCliente, Nombre, SUM(Importe) As Importe
FROM Clientes C LEFT JOIN Ventas V USING (NumCliente)
GROUP BY 1, 2
REUNION A IZQUIERDA
NumCliente Nombre Importe
1 Consumidor final 5000
2 Alberto 2970
3 Pedro null
Pág  8
BASES DE DATOS
Ventas Clientes
NumVenta NumCliente Fecha Importe NumCliente Nombre
1 1 01/10/07 1000 1 Consumidor final
2 1 03/10/07 1500 2 Alberto
3 2 03/10/07 1250 3 Pedro
4 1 10/10/07 2500
5 2 16/10/07 1720
Importes totales vendidos a todos los clientes.
SELECT C.NumCliente, Nombre, COALESCE(SUM(Importe), 0) As Importe
FROM Ventas V RIGHT JOIN Clientes C USING (NumCliente)
GROUP BY 1, 2
REUNION A DERECHA
NumCliente Nombre Importe
1 Consumidor final 5000
2 Alberto 2970
3 Pedro 0
Pág  9
BASES DE DATOS
Ventas Clientes
NumVenta NumCliente Fecha Importe NumCliente Nombre
1 1 01/10/07 1000 1 Consumidor final
2 1 03/10/07 1500 2 Alberto
3 2 03/10/07 1250 3 Pedro
4 1 10/10/07 2500
5 2 16/10/07 1720
Listado alfabético con importes totales vendidos a todos los clientes.
SELECT C.NumCliente, Nombre, COALESCE(SUM(Importe), 0) As Importe FROM 
Clientes C LEFT JOIN Ventas V USING (NumCliente)
GROUP BY 1, 2
ORDER BY Nombre
REUNION A IZQUIERDA + ORDEN
NumCliente Nombre Importe
2 Alberto 2970
1 Consumidor final 5000
3 Pedro 0
Pág  10
BASES DE DATOS
Ventas Clientes
NumVenta NumCliente Fecha Importe NumCliente Nombre
1 1 01/10/07 1000 1 Consumidor final
2 1 03/10/07 1500 2 Alberto
3 2 03/10/07 1250 3 Pedro
4 1 10/10/07 2500
5 2 16/10/07 1720
Ranking de clientes por importes totales vendidos.
SELECT C.NumCliente, Nombre, COALESCE(SUM(Importe), 0) As Importe FROM 
Clientes C LEFT JOIN Ventas V USING (NumCliente)
GROUP BY 1, 2
ORDER BY 3 DESC
REUNION A IZQUIERDA + ORDEN
NumCliente Nombre Importe
1 Consumidor final 5000
2 Alberto 2970
3 Pedro 0
Pág  11
BASES DE DATOS
Ventas Clientes
NumVenta NumCliente Fecha Importe NumCliente Nombre NumCiudad
1 1 01/10/07 1000 1 Consumidor final 1
2 1 03/10/07 1500 2 Alberto 2
3 2 03/10/07 1250 3 Pedro 1
4 1 10/10/07 2500 Ciudades
5 2 16/10/07 1720 NumCiudad Ciudad
1 Salta
2 Jujuy
SELECT V.NumVenta, V.Fecha, C.Nombre AS Cliente, C2.Nombre AS Ciudad 
FROM Ventas V INNER JOIN Clientes C USING (NumCliente) INNER JOIN Ciudades C2 
USING (NumCiudad)
WHERE C.Fecha >= ‘20071001’ AND C.Fecha < ‘20071008’
REUNION DE TRES TABLAS
NumVenta Fecha Cliente Ciudad
1 01/10/07 Consumidor final Salta
2 03/10/07 Consumidor final Salta
3 03/10/07 Alberto Jujuy
Pág  12
BASES DE DATOS
Preguntas

Otros materiales