0% found this document useful (0 votes)
63 views3 pages

SQL Server 2000 Aggregate Functions Guide

This document provides information on using aggregate functions in SQL queries. It defines aggregate functions as functions that generate summary values from multiple rows or columns. It lists common aggregate functions like SUM, AVG, COUNT, MIN, and MAX. It explains that aggregate functions process all selected values in a column to produce a single result value. It also discusses how aggregate functions can be used with WHERE and GROUP BY clauses to operate on subsets of rows.

Uploaded by

Kasey Owens
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views3 pages

SQL Server 2000 Aggregate Functions Guide

This document provides information on using aggregate functions in SQL queries. It defines aggregate functions as functions that generate summary values from multiple rows or columns. It lists common aggregate functions like SUM, AVG, COUNT, MIN, and MAX. It explains that aggregate functions process all selected values in a column to produce a single result value. It also discusses how aggregate functions can be used with WHERE and GROUP BY clauses to operate on subsets of rows.

Uploaded by

Kasey Owens
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Accessing and Changing Relational Data (SQL Server 2000)

Using Aggregate Functions in the Select List


Aggregate functions (such as SUM, A!, C"U#$, C"U#$(%), MA&, and M'#) generate su((ar) values in
*uer) result sets+ An aggregate function (,ith the [Link] of C"U#$(%)) .rocesses all the selected
values in a single colu(n to .roduce a single result value+ Aggregate functions can /e a..lied to all ro,s
in a ta/le, to a su/set of the ta/le [Link] /) a 012R2 clause, or to one or (ore grou.s of ro,s in the
ta/le+ 0hen an aggregate function is a..lied, a single value is generated fro( each set of ro,s+
$his e-a(.le calculates the su( of )ear3to3date sales for all /oo4s in the titles ta/le5
USE pubs
SELECT SUM(ytd_sales)
FROM titles
1ere is the result set5
------------------
97446
(1 !"(s) a##e$ted)
0ith this *uer), )ou can find the average .rice of all /oo4s if .rices ,ere dou/led5
USE pubs
SELECT a%&(pi$e ' ()
FROM titles
1ere is the result set5
------------------
(9)*+
(1 !"(s) a##e$ted)
$he ta/le sho,s the s)nta- of the aggregate functions and their results (expression is al(ost al,a)s a
colu(n na(e)+
Aggregate function Result
SUM(6ALL 7 D'S$'#C$8 expression) $otal of the values in the nu(eric [Link]
A!(6ALL 7 D'S$'#C$8 expression) Average of the values in the nu(eric
[Link]
C"U#$(6ALL 7 D'S$'#C$8 expression) #u(/er of values in the [Link]
C"U#$(*) #u(/er of selected ro,s
MA&(expression) 1ighest value in the [Link]
M'#(expression) Lo,est value in the [Link]
SUM, A!, C"U#$, MA&, and M'# ignore null values9 C"U#$(%) does not+
$he [Link] 4e),ord D'S$'#C$ can /e used ,ith SUM, A!, and C"U#$ to eli(inate [Link] values
/efore an aggregate function is a..lied (the default is ALL)+
SUM and A! can /e used onl) ,ith nu(eric colu(ns, for e-a(.le int, smallint, tinyint, decimal,
numeric, float, real, money, and smallmoney data t).es+ M'# and MA& cannot /e used ,ith bit data
t).es+ Aggregate functions other than C"U#$(%) cannot /e used ,ith text and image data t).es+
0ith these [Link], aggregate functions can /e used ,ith an) t).e of colu(n+ :or e-a(.le, in a
character data t).e colu(n, use M'# ((ini(u() to find the lo,est value (the one closest to the
/eginning of the [Link]/et)5
USE pubs
SELECT M,-(au_l.a/e)
FROM aut0!s
1ere is the result set5
------------------
1e..et
(1 !"(s) a##e$ted)
$he result t).e returned /) an aggregate function (a) have a larger .recision than the [Link] so that
the result t).e is large enough to hold the aggregated result value+ :or e-a(.le, the SUM or A!
functions return an int value ,hen the data t).e of the [Link] is smallint or tinyint+ :or (ore
infor(ation a/out the data t).e returned /) an aggregate function, see the [Link] for the function in
Microsoft; SQL Server< 2000 $ransact3SQL Reference+
Note $he [Link] for state(ents, involving M'# or MA& on character colu(ns, [Link] on the collation
chosen during installation+ :or (ore infor(ation a/out the effects of different collations, see SQL Server
Collation :unda(entals+
0hen aggregate functions are used in a select list, the select list can contain onl)5

Aggregate functions+

![Link] colu(ns fro( a !R"U= >? clause+

An [Link] that returns the sa(e value for ever) ro, in the result set, such as a constant+
:or (ore infor(ation a/out generating aggregate values for result sets containing ([Link] ro,s, see
![Link] Ro,s ,ith !R"U= >?+
Aggregate functions cannot /e used in a 012R2 clause+ 1o,ever, a S2L2C$ state(ent ,ith aggregate
functions in its select list often includes a 012R2 clause that restricts the ro,s to ,hich the aggregate
function is a..lied+ 'f a S2L2C$ state(ent includes a 012R2 clause (/ut not a !R"U= >? clause), an
aggregate function .roduces a single value for the su/set of ro,s [Link] /) the 012R2 clause+ $his is
true ,hether it is [Link] on all ro,s in a ta/le or on a su/set of ro,s defined /) a 012R2 clause+
Such a function is called a scalar aggregate+
$his *uer) returns the average advance and the su( of )ear3to3date sales for /usiness /oo4s onl)5
USE pubs
SELECT 234(ad%a.$e)5 SUM(ytd_sales)
FROM titles
67ERE type 8 9busi.ess9
1ere is the result set5
--------- -------
65(:1)(* +;7::
(1 !"(s) a##e$ted)
?ou can use (ore than one aggregate function in the sa(e select list and .roduce (ore than one scalar
aggregate in a single S2L2C$ state(ent+
See Also
Aggregate :unctions
S2L2C$

You might also like