Está en la página 1de 3

UTEC C02/2019 BAS2-I01

Utilización y consideraciones con cláusula Group


by y Order by
Introducción
En la utilización de group by es importante tener la claridad del resultado retornado, porque dependiendo de las
diferentes relaciones que establece con las tablas así es el resultado y puede no significar lo que se requiere, por lo cual
en esta guía se expone unos ejemplos que en el desarrollo de clase se validaran por ello la importancia que tome nota
de lo expuesto. Además, se analizará la cláusula order by dentro de una consulta para comprender su funcionamiento
y su importante, así como comprender su forma de operar. Por ultimo se realizara un análisis de los datos en Excel hoja
electrónica de Microsoft.

Fuente de datos a utilizar: AdventureWorks2008 o AdventureWorks2012

Group by
Lista número de productos por categoría.
select f.Name, count(d.ProductID) as [#Productos]
from Production.Product d
inner join Production.ProductSubcategory e on e.ProductSubcategoryID=d.ProductSubcategoryID
inner join Production.ProductCategory f on e.ProductCategoryID=f.ProductCategoryID
group by f.Name

NERM Pagina 1 de 3
UTEC C02/2019 BAS2-I01

Lista numero de productos comprados por categoria.


select e.Name, count(c.ProductID) as [#Productos]
from Purchasing.PurchaseOrderHeader a
inner join Purchasing.PurchaseOrderDetail b on b.PurchaseOrderID=a.PurchaseOrderID
inner join Production.Product c on b.ProductID=c.ProductID
inner join Production.ProductSubcategory d on d.ProductSubcategoryID=c.ProductSubcategoryID
inner join Production.ProductCategory e on e.ProductCategoryID=d.ProductCategoryID
group by e.Name

Lista número de subcategorías y numero de productos por categoría.


select f.Name, count(e.ProductSubcategoryID) as [#SubCategorias],
count(d.ProductID) as [#Productos]
from Production.Product d
inner join Production.ProductSubcategory e on e.ProductSubcategoryID=d.ProductSubcategoryID
inner join Production.ProductCategory f on e.ProductCategoryID=f.ProductCategoryID
group by f.Name

NERM Pagina 2 de 3
UTEC C02/2019 BAS2-I01

Order by
Listado de lo total comprado en año y mes por categoría.
select e.Name as [Nombre Categoria],
year(a.OrderDate) as [Año],
month(a.OrderDate) as Mes,
sum(a.TotalDue) as Total
from Purchasing.PurchaseOrderHeader a
inner join Purchasing.PurchaseOrderDetail b on b.PurchaseOrderID=a.PurchaseOrderID
inner join Production.Product c on b.ProductID=c.ProductID
inner join Production.ProductSubcategory d on d.ProductSubcategoryID=c.ProductSubcategoryID
inner join Production.ProductCategory e on e.ProductCategoryID=d.ProductCategoryID
group by e.Name, year(a.OrderDate), month(a.OrderDate)

Análisis.
Realizando análisis de datos con Excel al utilizar la siguiente consulta:
select f.ProductCategoryID, f.Name, e.ProductSubcategoryID, e.Name, d.ProductID, d.Name
from Production.Product d
inner join Production.ProductSubcategory e on e.ProductSubcategoryID=d.ProductSubcategoryID
inner join Production.ProductCategory f on e.ProductCategoryID=f.ProductCategoryID

NERM Pagina 3 de 3

También podría gustarte