Sie sind auf Seite 1von 2

Examen consultas:

1. Total facturado agrupado por categoras para el ao 1996


SELECT c.CategoryName, SUM(d.UnitPrice * d.Quantity) as Total_Facturado FROM Categories c JOIN Products p ON c.CategoryID = P.CategoryID JOIN [Order Details] d ON p.ProductID = d.ProductID JOIN Orders o ON o.OrderID = d.OrderID WHERE YEAR(o.OrderDate)=1996 GROUP BY c.CategoryName 2. Total facturado agrupado por categoras para el ao 1996 y 1998 SELECT c.CategoryName,(SELECT SUM(d.UnitPrice * d.Quantity) FROM Categories ca JOIN Products p ON ca.CategoryID = P.CategoryID JOIN [Order Details] d ON p.ProductID = d.ProductID JOIN Orders o ON o.OrderID = d.OrderID and ca.CategoryName=c.CategoryName WHERE YEAR(o.OrderDate)=1996) Total_Facturado_1996 ,(SELECT SUM(d.UnitPrice * d.Quantity) FROM Categories ca JOIN Products p ON ca.CategoryID = P.CategoryID JOIN [Order Details] d ON p.ProductID = d.ProductID JOIN Orders o ON o.OrderID = d.OrderID and ca.CategoryName=c.CategoryName WHERE YEAR(o.OrderDate)=1998) Total_Facturado_1998 FROM Categories c

3. Liste el total facturado por cliente y ao.


SELECT c.ContactName,YEAR(o.OrderDate) AS AO,SUM(od.UnitPrice*od.Quantity) AS TOTAL_FACTURADO FROM dbo.Customers c INNER JOIN dbo.Orders o on o.CustomerID=c.CustomerID INNER JOIN [Order Details] od on od.OrderID=o.OrderID GROUP BY c.ContactName,YEAR(o.OrderDate)

4. liste el total facturado por pas de cliente agrupado por ao


select cu.ContactName,YEAR(o.OrderDate)as 'AO',cu.Country,SUM(od.UnitPrice*od.Quantity) from Customers cu inner join Orders o on o.CustomerID=cu.CustomerID inner join [Order Details] od on od.OrderID=o.OrderID group by cu.ContactName,YEAR(o.OrderDate),cu.Country

5. liste todos los productos cuyo precio sea menor q el precio promedio
select ProductName,UnitPrice from dbo.Products where UnitPrice < (select AVG(UnitPrice) from Products) 6. liste los clientes (ContactName,CustomerDes) que hayan realizado alguna compra select Cu.ContactName,Cd.CustomerDesc from CustomerDemographics Cd inner join CustomerCustomerDemo Cc on Cc.CustomerTypeID=Cd.CustomerTypeID inner join Customers Cu on Cu.CustomerID= Cc.CustomerID --inner join Orders o on o.CustomerID = Cu.CustomerID where Cu.CustomerID in (select oo.CustomerID from Orders oo)

7. liste los nombres de los proovedores cuyos productos nunca se han vendido
select s.ContactName, p.ProductName from dbo.Suppliers s inner join Products p on p.SupplierID=s.SupplierID --inner join [Order Details] od on od.ProductID=p.ProductID where p.ProductID NOT IN (select oo.ProductID from [Order Details] oo

8. obtener el stock medio de los productos de cada proveedor mostrando el nombre del proveedor. Use subconsultas
select s.ContactName,(select AVG (UnitsInStock) from dbo.Products p where p.SupplierID=s.SupplierID)as stock_prom from dbo.Suppliers s

9.

obtener el nombre de los proveedores que ofrezcan productos cuyo precio medio sea mayor k 130.00

select ContactName from dbo.Suppliers s where 130 < (select AVG(UnitPrice) from Products p where p.SupplierID=s.SupplierID)

10. aplicar un descuento a todos los productos del 20% al precio unitario.
update dbo.Products set UnitPrice*=0.8

Si tienen otro tipo de solucin mas optima por favor compartan sus ideas

Das könnte Ihnen auch gefallen