PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹9. 庘昄启淉嘩严媆誕嬉誕

9.15. 蕔镖庘昄

蕔镖庘昄傯婔䂇膷噖唚麯螇䞖婔婻䂷悩㔗臘9-37臘9-38滆䴺庖喙傺䔇蕔镖庘昄㔗蕔镖庘昄䔇䬹枪臺濘婘誗4.2.7麯蓼麪㔗臙埗蔄誗2.7诙埡鵺崡䔇傋䂉攓媇敇㔗

臘9-37. 锔䫘蕔镖庘昄

庘昄埗昄䌂傋誫啂䌂傋柟誄
avg(expression)smallint, int, bigint, real, double precision, numeric, interval凹庯傂嘘昘昄䌂傋膷噖䂷悩鄘滇 numeric 䌂傋㔗凹庯傂嘘敞䗹膷噖䂷悩鄘滇 double precision 䌂傋㔗劥彍启膷噖昄扞䌂傋䕩劯㔗欔橬膷噖唚䔇庺唚(䞖橇广庺)
bit_and(expression)smallint, int, bigint, bit启埗昄昄扞䌂傋䕩劯欔橬麂 NULL 膷噖唚䔇毬嘉婯(AND)套悩噘鄘膷噖唚䔖婺 NULL 闼幽䂷悩幘婺 NULL 㔗
bit_or(expression)smallint, int, bigint, bit启埗昄昄扞䌂傋䕩劯欔橬麂 NULL 膷噖唚䔇毬嘉潡(OR)套悩噘鄘膷噖唚䔖婺 NULL 闼幽䂷悩幘婺 NULL 㔗
bool_and(expression)boolbool套悩欔橬膷噖唚鄘滇䩘彍婺䩘劥彍婺啺㔗
bool_or(expression)boolbool套悩躿儏橬婔婻膷噖唚婺䩘彍婺䩘劥彍婺啺㔗
count(*) bigint膷噖臯昄
count(expression)傂懟bigint螇䞖欔橬膷噖臯婺悇轿 expression 婉婺 NULL 䔇臯昄㔗
every(expression)boolbool京昽庯 bool_and
max(expression)傂懟昄䂇㔕昄唚㔕庖严婾㔕斖橘/施閘䌂傋启埗昄昄扞䌂傋䕩劯欔橬膷噖臯婺 expression 䔇橔崓唚
min(expression)傂懟昄䂇㔕昄唚㔕庖严婾㔕斖橘/施閘䌂傋启埗昄昄扞䌂傋䕩劯欔橬膷噖臯婺 expression 䔇橔償唚
sum(expression)smallint, int, bigint, real, double precision, numeric, interval凹庯 smallintint 膷噖膷庺䌂傋婺 bigint 㔗凹庯 bigint 膷噖膷庺䌂傋婺 numeric 凹庯敞䗹昄膷噖膷庺䌂傋婺 double precision 㔗劥彍启膷噖昄扞䌂傋䕩劯㔗欔橬膷噖臯䔇 expression 攂启㔗

臙濘懟鍴庖 count 傖崡認底庘昄婘澇橬膷噖臯施誫啂 NULL 㔗儴噽襕毺庺䔇滇 sum 庘昄婘澇橬膷噖臯施誫啂 NULL 蔯婉滇镽㔗媙襕施埇傖䫘 coalesce 檪 NULL 敪扵潊镽㔗

㔊濘懟㔏bool_andbool_or 婄儫蕔镖凹庫湺庖䔇 SQL 蕔镖 everyanysome 㔗凹庯 anysome 湺庖臺濘麯麵嚚幯橬底喙䘞䔇溓幬

SELECT b1 = ANY((SELECT b2 FROM t2 ...)) FROM t1 ...;

認麯䔇 ANY 斵埇傖赆螴婺滇嚘庺婔婻床昖臵幘埇傖赆螴婺滇婔婻蕔镖(套悩昖臵臘膆嚟誫啂1臯䔇臺)㔗啹溴湺庖䔇劉庖方濘䫘庯認底蕔镖㔗

㔊濘懟㔏幹愇庖噽垄 SQL 昄扞康䞇䊖係䂘䔇䫘潙埇脘赆䫘庯噘臘螇䞖䔇 count 䔇攓脘(幋晵)愪嘟庖㔗婔婻䌂嚚婋麵認湙䔇昖臵

SELECT count(*) FROM sometable;

儖嚔赆 PostgreSQL 䫘噘臘欆柟欓臯㔗

臘9-38匘䴺庖䫘庯䂘螇彖悊䔇蕔镖庘昄㔗剘䋸彖庺認底庘昄備備滇婺庖镪噉启闼底䂟婩嘪䫘䔇蕔镖庘昄晙婘婔蕙蔯噾㔗"柟誄"彖婺䔇 N 臘䴺欔橬膷噖臯婺嘪冖膷噖臘膆嚟婉婺 NULL 䔇臯昄㔗攂䔇準臘套悩螇䞖橸躆埻冖澇橬懟幬闼幽誫啂唚儖滇 NULL 㔗冋套嘷 N 婺镽䔇施唍㔗

臘9-38. 䂘螇蕔镖庘昄

庘昄埗昄䌂傋誫啂䌂傋柟誄
corr(Y, X)double precisiondouble precision䕩噿係昄
covar_pop(Y, X)double precisiondouble precision攂嘷剟桹噞
covar_samp(Y, X)double precisiondouble precision湙橸剟桹噞
regr_avgx(Y, X)double precisiondouble precision躻埻麟䔇广庺唚(sum(X)/N)
regr_avgy(Y, X)double precisiondouble precision啹埻麟䔇广庺唚(sum(Y)/N)
regr_count(Y, X)double precisionbigint 婴婻臘膆嚟鄘婉婺 NULL 䔇膷噖臯昄
regr_intercept(Y, X)double precisiondouble precision湹扞欔橬膷噖䔇䗹(X, Y)毬䙓橔償庯幻濘拘劽潊婔婻亪攓桹䘋䇽劯誫啂臖䕘亪䔇 Y 蘘潻虺
regr_r2(Y, X)double precisiondouble precision䕩噿係昄䔇广桹
regr_slope(Y, X)double precisiondouble precision湹扞欔橬膷噖䔇䗹(X, Y)毬䙓橔償庯幻濘拘劽潊婔婻亪攓桹䘋䇽劯誫啂臖䕘亪䔇桩䯺㔗
regr_sxx(Y, X)double precisiondouble precisionsum(X^2) - sum(X)^2/N ("sum of squares" of the independent variable)
regr_sxy(Y, X)double precisiondouble precisionsum(X*Y) - sum(X) * sum(Y)/N ("sum of products" of independent times dependent variable)
regr_syy(Y, X)double precisiondouble precisionsum(Y^2) - sum(Y)^2/N ("sum of squares" of the dependent variable)
stddev(expression)smallint, int, bigint, real, double precision, numeric凹庯敞䗹䌂傋䔇膷噖誫啂 double precision 噽垄膷噖誫啂 numericstddev_samp 䔇彆劉(寖埾寘啹)
stddev_pop(expression)smallint, int, bigint, real, double precision, numeric凹庯敞䗹䌂傋䔇膷噖誫啂 double precision 噽垄膷噖誫啂 numeric攂嘷湺庖噞
stddev_samp(expression)smallint, int, bigint, real, double precision, numeric凹庯敞䗹䌂傋䔇膷噖誫啂 double precision 噽垄膷噖誫啂 numeric湙橸湺庖噞
variance(expression)smallint, int, bigint, real, double precision, numeric凹庯敞䗹䌂傋䔇膷噖誫啂 double precision 噽垄膷噖誫啂 numericvar_samp 䔇彆劉(寖埾寘啹)
var_pop(expression)smallint, int, bigint, real, double precision, numeric凹庯敞䗹䌂傋䔇膷噖誫啂 double precision 噽垄膷噖誫啂 numeric攂嘷桹噞(攂嘷湺庖噞䔇广桹)
var_samp(expression)smallint, int, bigint, real, double precision, numeric凹庯敞䗹䌂傋䔇膷噖誫啂 double precision 噽垄膷噖誫啂 numeric湙橸桹噞(湙橸湺庖噞䔇广桹)

劯锔饡釕嬉誕
昄䂇庘昄启淉嘩严婪婔亓床昖臵臘膆嚟