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 e-ce.tion 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 s.ecified /) 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 e-.ression
A!(6ALL 7 D'S$'#C$8 expression) Average of the values in the nu(eric
e-.ression
C"U#$(6ALL 7 D'S$'#C$8 expression) #u(/er of values in the e-.ression
C"U#$(*) #u(/er of selected ro,s
MA&(expression) 1ighest value in the e-.ression
M'#(expression) Lo,est value in the e-.ression
SUM, A!, C"U#$, MA&, and M'# ignore null values9 C"U#$(%) does not+
$he o.tional 4e),ord D'S$'#C$ can /e used ,ith SUM, A!, and C"U#$ to eli(inate du.licate 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 e-ce.tions, 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 al.ha/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 in.uts 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 in.uts is smallint or tinyint+ :or (ore
infor(ation a/out the data t).e returned /) an aggregate function, see the to.ic for the function in
Microsoft; SQL Server< 2000 $ransact3SQL Reference+
Note $he out.ut for state(ents, involving M'# or MA& on character colu(ns, de.ends 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+
!rou.ing colu(ns fro( a !R"U= >? clause+
An e-.ression 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 (ulti.le ro,s, see
!rou.ing 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 s.ecified /) the 012R2 clause+ $his is
true ,hether it is o.erating 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$