Professional Documents
Culture Documents
select * into BCK_1 from Products where UnitsOnOrder<20 and UnitPrice <10 or
UnitsInStock < 7
--CONSULTA 2
select ShipCity,count(1)'ORDENES POR CIUDAD',ShipCountry,sum(freight)'FLETE'
into BCK_2 from orders
group by ShipCity,ShipCountry having count(1) between 20 and 40
--CONSULTA 3
select top 5 percent
OrderID,(sum(UnitPrice*Quantity))'SUBTOTAL',sum((UnitPrice*Quantity)*(1-
Discount))'VALOR REAL' into BCK_3 from [Order Details]
group by OrderID having sum(UnitPrice*Quantity)>2500
select * from BCK_1
select * from BCK_2
select * from BCK_3
--CONSULTA 1
select * from orders
select * from Customers
select * from Employees
select * from Categories
select * from Products
select * from [Order Details]
go
create view v_ordencompleta as
select
o.OrderID,c.CompanyName,e.LastName,e.FirstName,o.OrderDate,o.ShipName,o.Freig
ht,p.ProductName,p.UnitPrice,
d.Quantity,d.Discount,(d.UnitPrice*d.Quantity)*(1-d.Discount)'IMPORTE' from Orders
o
inner join Customers c on o.CustomerID=c.CustomerID
inner join Employees e on o.EmployeeID=e.EmployeeID
inner join [Order Details] d on o.OrderID=d.OrderID
inner join Products p on d.ProductID=p.ProductID
inner join Categories ca on p.CategoryID=ca.CategoryID
go
select LastName,count(OrderID)'Cantidad' from v_ordencompleta group by LastName
having LastName='Buchanan'
select top 1 OrderID,sum(Quantity)'Cantidad' from v_ordencompleta group by OrderID
order by sum(Quantity) desc
select top 1 year(OrderDate)'Ao',sum(Quantity)'Cantidad' from v_ordencompleta
group by year(OrderDate) order by sum(Quantity) desc
select top 1 CompanyName,count(1)'Cantidad Ordenes' from v_ordencompleta group
by CompanyName order by count(1) desc
select top 1 CompanyName, sum(IMPORTE)'Gasto' from v_ordencompleta group by
CompanyName order by sum(IMPORTE) desc
select top 1 ProductName,sum(Quantity)'Cantidad' from v_ordencompleta group by
ProductName order by sum(Quantity) desc
select top 1 ProductName,sum(importe)'Ingreso' from v_ordencompleta group by
ProductName order by sum(UnitPrice*Quantity) desc
go
create view v_producto as
select p.ProductName,s.CompanyName,c.CategoryName,p.UnitsInStock from Products
p
inner join Suppliers s on p.SupplierID=s.SupplierID
inner join Categories c on p.CategoryID=c.CategoryID
go
update Products set ProductName='Chai1' where ProductName='Chai'
update v_producto set ProductName='Chai' where ProductName='Chai1'
select * from Products
select * from v_producto
create table bco_Cliente(
codigo int identity(10001,1)primary key,
nombre varchar(80)unique not null,
fecha datetime default getdate(),
lineaCredito money default 0 check (lineaCredito>=0),
lineaDisponible money default 0 check (lineaDisponible>=0))
go
create table bco_Movimiento(
nro_Ope int identity primary key,
codCli int,
fechOpe datetime default getdate(),
cargo money default 0,
abono money default 0)
----numero 2----
select*from Shippers
go
-----numero 3------
select*from Products
go
exec pa_examen3
go
------numero 4------
create procedure
-PROCEDIMIENTOS ALMACENADOS
/*Crear un procedimiento almacenado al cual llamara pa_Listaproductos
el cual almacenara el codigo de producto, el nombre del producto para aquellos
productos
que no esten descontinuados*/
create proc pa_ListaProductos as
select ProductID,ProductName from Products
where Discontinued=0
order by ProductName
go
pa_ListaProductos --o
exec pa_ListaProductos
go
-- datos de los procedimientos almacenados
exec sp_helptext pa_ListaProductos
go
select * from sys.procedures
go
--sel
select * from sys.tables
go
/*Se le pide crear un procedimiento almacenado al cual llamara "pa_ListaClientes"
mediante el cual generara una copia total de la tabla customers a una tabla llamada
clientes*/
alter proc pa_ListaClientes as
if(select count(1) from sys.tables
where name='Clientes')>0
drop table Clientes
select * into clientes from Customers
select 'Se actualizo correctamente la tabla Clientes'
go
drop proc pa_ListaClientes
drop table clientes
exec pa_ListaClientes
select * from clientes
/* Crear un procedimiento almacenado al cual llamara "pa_ValorProducto" que
selecione
el codigo de la orden el nombre del producto y el total aplicando el descuento
como "VALOR TOTAL" de cada uno de los productos que componen la orden */
create procedure pa_ValorProducto as
select od.OrderID, p.ProductName,
((od.UnitPrice*od.Quantity)*(1-od.Discount)) AS 'VALOR TOTAL'
from [Order Details] AS OD
JOIN Products AS P ON p.ProductID=od.ProductID
go
exec pa_ValorProducto
go
/*INGRESANDO VALORES MEDIANTE UN
PROCEDIMIENTO ALMACENADO*/
/* SALUDO*/
alter proc saludo
@nombre varchar(20), @apellido varchar(20)
as
select 'Hola mi nombre es '+@nombre+' '+ @apellido+' mucho gusto'
go
exec saludo Gilmar, Bayona