Вы находитесь на странице: 1из 9

Sesin 12

Agrupando y
Sumarizando Data

Overview

Clasula TOP n
Funciones de Agregamiento
GROUP BY
COMPUTE y COMPUTE BY

Clasula TOP n

Listar slo los primeros n Registros


Especificar Rango de Valores en ORDER BY
Opcin WITH TIES
Example
Example 11

USE
USE northwind
northwind
SELECT
SELECT TOP
TOP 55 orderid,
orderid, productid,
productid, quantity
quantity
FROM
FROM [order
[order details]
details]
ORDER
ORDER BY
BY quantity
quantity DESC
DESC
GO
GO
Example 2

Example 2
USE
northwind
USE northwind
SELECT
SELECT TOP
TOP 55 WITH
WITH TIES
TIES orderid,
orderid, productid,
productid, quantity
quantity
FROM
FROM [order
[order details]
details]
ORDER
ORDER BY
BY quantity
quantity DESC
DESC
GO
GO

Funciones de Agregamiento
Aggregate
Aggregatefunction
function

Description
Description

AVG
AVG

Average
Averageof
ofvalues
valuesin
inaanumeric
numericexpression
expression

COUNT
COUNT

Number
Numberof
ofvalues
valuesin
inan
anexpression
expression

COUNT
COUNT(*)
(*)

Number
Numberof
ofselected
selectedrows
rows

MAX
MAX

Highest
Highestvalue
valuein
inthe
theexpression
expression

MIN
MIN

Lowest
Lowestvalue
valuein
inthe
theexpression
expression

SUM
SUM

Total
Totalvalues
valuesin
inaanumeric
numericexpression
expression

STDEV
STDEV

Statistical
Statisticaldeviation
deviationof
ofall
allvalues
values

STDEVP
STDEVP

Statistical
Statisticaldeviation
deviationfor
forthe
thepopulation
population

VAR
VAR

Statistical
Statisticalvariance
varianceof
ofall
allvalues
values

VARP
VARP

Statistical
Statisticalvariance
varianceof
ofall
allvalues
valuesfor
forthe
thepopulation
population

Usando Funciones con NULL


Muchas Funciones de Agregamiento Ignoran NULL
COUNT(*) considera NULL

USE
USE northwind
northwind
SELECT
SELECT COUNT
COUNT (*)
(*)
FROM
FROM employees
employees
GO
GO

Example
Example 11

USE
USE northwind
northwind
SELECT
SELECT COUNT(reportsto)
COUNT(reportsto)
FROM
FROM employees
employees
GO
GO

Example
Example 22

GROUP BY
GROUP BY
GROUP BY con HAVING

GROUP BY
USE northwind
SELECT productid, orderid
,quantity
FROM orderhist
GO

USE northwind
SELECT productid
,SUM(quantity) AS total_quantity
FROM orderhist
GROUP BY productid
GO

productid
productid orderid
orderid quantity
quantity
11
11

11
11

55
10
10

22
22

11
22

10
10
25
25

33
33

11
22

15
15
30
30

productid
productid total_quantity
total_quantity

Only rows that


satisfy the WHERE
clause are grouped

11
22

15
15
35
35

33

45
45

productid
productid total_quantity
total_quantity
22

35
35

USE northwind
SELECT productid
,SUM(quantity) AS total_quantity
FROM orderhist
WHERE productid = 2
GROUP BY productid
GO

GROUP BY con HAVING


USE northwind
SELECT productid, orderid
,quantity
FROM orderhist
GO
productid
productid orderid
orderid quantity
quantity
11
11

11
11

55
10
10

22
22

11
22

10
10
25
25

33
33

11
22

15
15
30
30

USE northwind
SELECT productid, SUM(quantity)
AS total_quantity
FROM orderhist
GROUP BY productid
HAVING SUM(quantity)>=30
GO

productid
productid total_quantity
total_quantity
22
33

35
35
45
45

Lab 12: Agrupando y Sumarizando Data

Вам также может понравиться