Upload
kasey-owens
View
214
Download
0
Embed Size (px)
Citation preview
8/10/2019 SQL.usingAggregateFuncInSelectList
http://slidepdf.com/reader/full/sqlusingaggregatefuncinselectlist 1/3
Accessing and Changing Relational Data (SQL Server 2000)
Using Aggregate Functions in the Select ListAggregate functions (such as SUM, A!, C"U#$, C"U#$(%), MA&, and M'#) generate suar values in
*uer result sets+ An aggregate function (ith the e-ce.tion of C"U#$(%)) .rocesses all the selected
values in a single colun to .roduce a single result value+ Aggregate functions can /e a..lied to all ros
in a ta/le, to a su/set of the ta/le s.ecified / a 1R clause, or to one or ore grou.s of ros in the
ta/le+ hen an aggregate function is a..lied, a single value is generated fro each set of ros+
$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)
ith 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 shos the snta- of the aggregate functions and their results (expression is alost alas a
colun nae)+
Aggregate function Result
SUM(6ALL 7 D'S$'#C$8 expression) $otal of the values in the nueric e-.ression
A!(6ALL 7 D'S$'#C$8 expression) Average of the values in the nuerice-.ression
C"U#$(6ALL 7 D'S$'#C$8 expression) #u/er of values in the e-.ression
C"U#$(*) #u/er of selected ros
MA&(expression) 1ighest value in the e-.ression
M'#(expression) Loest value in the e-.ression
SUM, A!, C"U#$, MA&, and M'# ignore null values9 C"U#$(%) does not+
$he o.tional 4eord D'S$'#C$ can /e used ith SUM, A!, and C"U#$ to eliinate du.licate values
/efore an aggregate function is a..lied (the default is ALL)+
SUM and A! can /e used onl ith nueric coluns, 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+
8/10/2019 SQL.usingAggregateFuncInSelectList
http://slidepdf.com/reader/full/sqlusingaggregatefuncinselectlist 2/3
ith these e-ce.tions, aggregate functions can /e used ith an t.e of colun+ :or e-a.le, in a
character data t.e colun, use M'# (iniu) to find the loest 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
------------------
e..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
inforation 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 stateents, involving M'# or MA& on character coluns, de.ends on the collation
chosen during installation+ :or ore inforation a/out the effects of different collations, see SQL Server
Collation :undaentals+
hen aggregate functions are used in a select list, the select list can contain onl5
• Aggregate functions+
• !rou.ing coluns fro a !R"U= >? clause+
• An e-.ression that returns the sae value for ever ro in the result set, such as a constant+
:or ore inforation a/out generating aggregate values for result sets containing ulti.le ros, see
!rou.ing Ros ith !R"U= >?+
Aggregate functions cannot /e used in a 1R clause+ 1oever, a SLC$ stateent ith aggregate
functions in its select list often includes a 1R clause that restricts the ros to hich the aggregate
function is a..lied+ 'f a SLC$ stateent includes a 1R clause (/ut not a !R"U= >? clause), an
aggregate function .roduces a single value for the su/set of ros s.ecified / the 1R clause+ $his is
true hether it is o.erating on all ros in a ta/le or on a su/set of ros defined / a 1R clause+
Such a function is called a scalar aggregate+
$his *uer returns the average advance and the su of ear3to3date sales for /usiness /oo4s onl5
USE pubs
SELECT 23(ad%a.$e)5 SUM(ytd_sales)
FROM titles
ERE type 8 busi.ess
1ere is the result set5
--------- -------
65:1* +;7::
8/10/2019 SQL.usingAggregateFuncInSelectList
http://slidepdf.com/reader/full/sqlusingaggregatefuncinselectlist 3/3
(1 !"(s) a##e$ted)
?ou can use ore than one aggregate function in the sae select list and .roduce ore than one scalar
aggregate in a single SLC$ stateent+
See Also
Aggregate :unctions
SLC$