Beruflich Dokumente
Kultur Dokumente
go
-- listar los clinetes (customers) y el toptal de compras por ao
-- por cada ao mostrar el subtotal , al final el total general
select c.CustomerID,C.CompanyName, sum(O.Freight) as 'Total',
year(O.OrderDate)as 'Ao'
from Customers as C join Orders as O on C.CustomerID = O.CustomerID
group by C.CustomerID, C. CompanyName,year(O.OrderDate)
order by year(O.OrderDate),Total desc
GO
-- LISTAR
select c.CustomerID,C.CompanyName, sum(O.Freight) as 'Total',
year(O.OrderDate)as 'Ao'
from Customers as C join Orders as O on C.CustomerID = O.CustomerID
group by C.CustomerID, C. CompanyName,year(O.OrderDate)
order by year(O.OrderDate),Total desc
GO
-- Gestion 2 - 28/05/2015
-- CONSULTAS USANDO JOIN
-- SUBCONSULTAS
-- FUNCIONES DEFINIDAS POR EL USUARIO
-- TABLAS TEMPORALES
-- USO DE SP EN VISUAL STUDIOR
USE Northwind
GO
-- LISTAR LAS ORDENES DE AGOSTO 1997
-- INCLUYENDO EL CLIENTE Y EL EMPLEADO
SELECT O.OrderID AS 'N DE ORDEN',
O.OrderDate AS 'FECHA',
O.Freight AS 'MONTO',
e.LastName +SPACE(1)+ E.FirstName AS 'NOMBRE EMPLEADO',
-- EMPLEADO = e.LastName +SPACE(1)+ E.FirstName
C.ContactName AS 'NOMBRE USUARIO'
FROM Orders AS O
JOIN Employees AS E ON O.EmployeeID = e.EmployeeID
JOIN Customers AS C ON O.CustomerID=c.CustomerID
WHERE YEAR(O.OrderDate)=1997 AND MONTH(O.OrderDate)=8
GO
-- SUBCONSULTAS
/* -- DOSTIPOS
1. IN LINE, INCLUIDA EN LA LISTA DE CAMPOS
DE UN SELECT
SELECT CAMPO1,(SELECT...), CAMPO4 FROM TABLA
2. EN LA CLAUSULA WHERE
SELECT CAMPO1,... CAMPO4 FROM TABLA WHERE
CAMPOfILTRO OPERADOR(SELECT ...)
*/
-- LISTADO DE LOS CLIENTES DE BRAZIL QUE COMPRARON EN 1998
-- primero: cuales son los codigos de los clientes de brazil
select C.CustomerID from Customers as C where country='Brazil'
go
-- listado de las ordenes de 1998 de los clientes de blazil
select c.CustomerID as 'Codigo',
c.CompanyName as 'Cliente',
count(o.OrderID ) as 'Cantidad de Orden',
sum(o.Freight) as 'Monto total',
YEAR (o.OrderDate) as 'Ao',
c.Country as 'Pais'
from Orders as o join Customers as C on O.CustomerID=C.CustomerID
where year(o.OrderDate) = 1998
and
o.CustomerID in (select C.CustomerID from Customers as C where country='Brazil')
group by C.CustomerID,c.CompanyName, year(o.OrderDate), C.Country
order by c.CompanyName
go
go
-- Insertar varios
insert into Aulas (AulasDescripcion)
Values ('Segundo grado'),('Tercer grado'),('Cuarto grado'),
('Quinto grado'),('Sexto grado')
go