You are on page 1of 3

--Abrir la base de datos use AdventureWorks2012 go select * from sys.tables--describe todas las tablas select * from sales.

SalesOrderHeader select SalesOrderID From sales.SalesOrderDetail --Agrupar por salesorderid Select top 5 SalesOrderID, count(*) [detalle], max(OrderQty) [CantMax], Min(OrderQty) [CantMin], Avg(UnitPrice) [promedioprecio] From sales.SalesOrderDetail where cast (SalesOrderID as char(5)) like '%[13579]' group by SalesOrderID having count (*)>3 and max(OrderQty)>4 Order by 2 Desc -ejemplo quiero agrupar por genero en mujeres y hombres ---Agrupamiento por color Select Color, count(*)[Cantidad_de_Producto] from Production.Product where color is not null group by Color having count(*) >10 --precio mas caro por producto y que sea mayor a cero --Cuando se evalua un tema se utiliza el having Select Color, Max(ListPrice)[PrecioAlto], min(ListPrice) [PrecioBajo] from Production.Product where color is not null group by Color having min(ListPrice) >0 --Select PersonType, Title, COUNT(*)[CantxTipoxTitle] from Person.Person where Title is not null group by PersonType, Title order by 3 desc Go --Ejemplo de como crear SENTENCIA UNION --Para 6000, 12000,19000 datos --Creando tablas a partir consultas Select BusinessEntityID,Title, FirstName,LastName INTO dbo.personalGen

from Person.Person where BusinessEntityID =99999 GO Select BusinessEntityID,Title, FirstName,LastName INTO dbo.personalSoph from Person.Person where BusinessEntityID < 6000 GO Select BusinessEntityID,Title, FirstName,LastName INTO dbo.personalConti from Person.Person where BusinessEntityID between 6000 AND 12000 go Select BusinessEntityID,Title, FirstName,LastName INTO dbo.personalChamp from Person.Person where BusinessEntityID between 12001 and 19000 GO Select BusinessEntityID,Title, FirstName,LastName INTO dbo.personalMiraf from Person.Person where BusinessEntityID between 6000 and 19000 GO --Union de tablas insert into personalGen Select BusinessEntityID, from personalGen UNION Select BusinessEntityID, from personalSoph UNION Select BusinessEntityID, from personalConti UNION Select BusinessEntityID, from personalChamp UNION Select BusinessEntityID, from personalMiraf

Title,FirstName,LastName Title,FirstName,LastName Title,FirstName,LastName Title,FirstName,LastName Title,FirstName,LastName

--Combinacion Interna (Inner Join) Select E.BusinessEntityID, P.FirstName, p.LastName, E.BirthDate, E.Gender, e.MaritalStatus from HumanResources.Employee E JOIN Person.Person P ON E.BusinessEntityID= P.BusinessEntityID Select DISTINCT P.ProductID,P.Name,p.ListPrice,OD.UnitPrice from sales.SalesOrderDetail OD JOIN production.Product P ON OD.ProductID=P.ProductID AND OD.UnitPrice= P.ListPrice --COMPROBRAR SELECT DISTINCT ProductID, UnitPrice FROM SALES.SalesOrderDetail

Select distinct ProductID, ListPrice from Production.Product --Combinacion Externa

You might also like