Sei sulla pagina 1di 3

EJERCICIOS PROPUESTOS DE SQL SERVER:

--17) Listar el nombre y apellido del empleado en una columna llamada Nom_Ape
select(LastName+
_ape,*FROMEmplo

space(1)+FirstName)asnom
yees

--18) Listar que productos se vendieron ms en el ao 1997:


Select [ProductName]as'producto',[QuantityPerUnit]as'detalles',[Quantity]as'cantidad',
[OrderDate]as'fecha'
from[Order Details]ASojoinProductsASaono.[ProductID]=a.[ProductID]
joinOrdersaseone.OrderID=o.OrderID
whereyear(OrderDate)= 1997 orderbycantidaddesc

--SUB CONSULTAS
--1. Listar los 10 peores clientes que han sido registrados en la Base de Datos
selectTOP 10 [CustomerID]As'Cdigo',[ContactName]As
'Cliente',[CompanyName]ASEmpresa,[Address]As'Direccin',(selectsum(freight)
fromOrdersAsOwhereO.CustomerID=C.CustomerID)As
'MontoComprado',CityAs'Ciudad'fromCustomersAsC
orderby'MontoComprado'ASC

--2. Listar los 10 productos ms vendidos en el supermercado en el ao 1998


selecttop 10 [ProductName]as'producto',[QuantityPerUnit]as'detalles',[Quantity]as'cantidad',
[OrderDate]as'fecha'
from[Order Details]ASojoinProductsASaono.[ProductID]=a.[ProductID]
joinOrdersaseone.OrderID=o.OrderID

whereyear(OrderDate)= 1998 orderbycantidaddesc

--3. Listarlas 3 categorasmsvendidas

selecttop 3 ca.CategoryName,COUNT(pro.CategoryID)ascantidadfromCategoriesasca
innerjoinProductsasproonpro.CategoryID=ca.CategoryID
groupbyca.CategoryNameorderbycantidaddesc

--4. Listar los 5 empleados que facturaron ms


Selecttop5(LastName+space(1)+FirstName)asempleado,SUM(d.UnitPrice*d.Quantity)as
Total_FacturadoFromEmployeesase
JOINOrderspone.EmployeeID=p.EmployeeID
JOIN"order details"donp.OrderID=d.OrderID
Groupby (LastName+space(1)+FirstName)orderbySUM(d.UnitPrice*d.Quantity)desc

--5. Esta subconsulta muestra los 5 mejores productos vendidos en el supermercado


selecttop 5 p.ProductName,sum(Quantity)ascantidad_vendidafromProductsasp
innerjoin[Order Details]asodonod.ProductID=p.ProductID
innerjoinOrdersasoono.OrderID=od.OrderID
groupbyp.ProductNameorderbycantidad_vendidadesc

--6. Mostrar una categora de clientes segn su poder de consumo


--a. Clase d 0-50
select[CustomerID]As'Cdigo',[ContactName]As
'Cliente',[CompanyName]ASEmpresa,[Address]As'Direccin',(selectsum(freight)
fromOrdersAsOwhereO.CustomerID=C.CustomerID)As
Consumo,CityAs'Ciudad'fromCustomersAsC
groupby[CustomerID],[ContactName],[CompanyName],[Address],city
having(selectsum(freight)
fromOrdersAsOwhereO.CustomerID=C.CustomerID)<50
orderbyconsumoasc
--b. Clase c + 50 -> 200
select[CustomerID]As'Cdigo',[ContactName]As
'Cliente',[CompanyName]ASEmpresa,[Address]As'Direccin',(selectsum(freight)
fromOrdersAsOwhereO.CustomerID=C.CustomerID)As
consumo,CityAs'Ciudad'fromCustomersAsC
groupby[CustomerID],[ContactName],[CompanyName],[Address],city
having(selectsum(freight)
fromOrdersAsOwhereO.CustomerID=C.CustomerID)>49 AND(selectsum(freight)
fromOrdersAsOwhereO.CustomerID=C.CustomerID)<200
orderbyconsumoasc
--c. Clase b +200 -> 5000
select[CustomerID]As'Cdigo',[ContactName]As
'Cliente',[CompanyName]ASEmpresa,[Address]As'Direccin',(selectsum(freight)
fromOrdersAsOwhereO.CustomerID=C.CustomerID)As
consumo,CityAs'Ciudad'fromCustomersAsC
groupby[CustomerID],[ContactName],[CompanyName],[Address],city
having(selectsum(freight)
fromOrdersAsOwhereO.CustomerID=C.CustomerID)>200 AND(selectsum(freight)
fromOrdersAsOwhereO.CustomerID=C.CustomerID)<5000
orderbyconsumoasc
--d. Clase a + 5000
select[CustomerID]As'Cdigo',[ContactName]As
'Cliente',[CompanyName]ASEmpresa,[Address]As'Direccin',(selectsum(freight)
fromOrdersAsOwhereO.CustomerID=C.CustomerID)As
consumo,CityAs'Ciudad'fromCustomersAsC
groupby[CustomerID],[ContactName],[CompanyName],[Address],city
having(selectsum(freight)
fromOrdersAsOwhereO.CustomerID=C.CustomerID)>5000
orderbyconsumoasc

Potrebbero piacerti anche