PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔媆誕嬉誕

CREATE AGGREGATE

劉䓄

CREATE AGGREGATE -- 垔幬婔婻桄䔇蕔镖庘昄

臺濘

CREATE AGGREGATE name ( input_data_type [ , ... ] ) (
    SFUNC = sfunc,
    STYPE = state_data_type
    [ , FINALFUNC = ffunc ]
    [ , INITCOND = initial_condition ]
    [ , SORTOP = sort_operator ]
)

婋麵滇斓臺濘

CREATE AGGREGATE name (
    BASETYPE = base_type,
    SFUNC = sfunc,
    STYPE = state_data_type
    [ , FINALFUNC = ffunc ]
    [ , INITCOND = initial_condition ]
    [ , SORTOP = sort_operator ]
)

柟誄

CREATE AGGREGATE 垔幬婔婻桄䔇蕔镖庘昄㔗婔底婩䫘䔇蕔镖庘昄噾䂟寙劆婘嘺䇔蘇傽寙麯庖婘誗9.15麯橬桺懼螄嘘㔗套悩嘹驔襕垔幬婔婻桄䌂傋潡驔襕婔婻誻澇橬柊冕䔇蕔镖庘昄認施 CREATE AGGREGATE 冪埇毐婪䫘婺㔗

套悩䂍庺庖婔婻昇嚟䔇劉庖(懫套 CREATE AGGREGATE myschema.myagg ...)闼幽臖蕔镖庘昄滇婘毺垔昇嚟婺录傺䔇㔗劥彍垄滇婘嘷嬉昇嚟婺录傺䔇㔗

婔婻蕔镖庘昄滇䫘垄䔇劉庖启膷噖昄扞䌂傋準湺臖䔇㔗劯婔昇嚟婺套悩婴婻蕔镖崇䊖䔇膷噖昄扞婉劯垄傸埇傖橬䕩劯䔇劉庖㔗婔婻蕔镖庘昄䔇膷噖昄扞䌂傋媙釂启欔橬劯婔昇嚟婺䔇捞锔庘昄䔇劉庖启膷噖䌂傋婉劯㔗

婔婻蕔镖庘昄滇䫘婔婻潡婴婻捞锔庘昄啔潊䔇婔婻䪽攕蘸扵庘昄 sfunc 启婔婻埇锬䔇橔䂽螇䞖庘昄 ffunc 㔗垄傸滇認湙嘪䫘䔇

sfunc( internal-state, next-data-values ) ---> next-internal-state
ffunc( internal-state ) ---> aggregate-value

PostgreSQL 录傺婔婻䌂傋婺 stype 䔇婘施埻麟㔗垄媺庻認婻蕔镖䔇嘷嬉喙鄘䪽攕㔗凹庯懟婻膷噖昄扞溇䕞鄘脄䫘䪽攕蘸扵庘昄螇䞖喙鄘䪽攕唚䔇桄昄唚㔗婘崇䊖垯欔橬昄扞劯脄䫘婔渇橔䂽崇䊖庘昄傖螇䞖蕔镖䔇誫啂唚㔗套悩澇橬橔䂽崇䊖庘昄彍儖橔劯䔇䪽攕唚嘷啔誫啂唚㔗

婔婻蕔镖庘昄誻埇脘柊冕婔婻彺哋溇傽幘儌滇喙鄘䪽攕唚䔇彺哋唚㔗認婻唚滇嘩婺婔婻䌂傋婺 text 䔇庖枕庻嗘婘昄扞康麯䔇婉誺垄傸媙釂滇䪽攕唚昄扞䌂傋䔇劽濘䔇崡鄘臘䯄嘵嚟䔇婩麟㔗套悩澇橬柊冕䪽攕闼幽䪽攕唚彺哋寡婺 NULL 㔗

套悩臖䪽攕蘸扵庘昄赆垔幬婺"strict"闼幽儌婉脘䫘 NULL 膷噖脄䫘垄㔗溴施蕔镖䔇欓臯套婋欔誄㔗婥橬傂嘘 NULL 膷噖唚䔇臯儖赆媘䘖(婉脄䫘溴庘昄幽婫媺䘍嬉婔婻䪽攕唚)㔗套悩彺哋䪽攕唚滇 NULL 闼幽婘丸婔婻劆橬麂 NULL 唚䔇臯婪嘪䫘丸婔婻埗昄唚敪扵䪽攕唚䇽劯䪽攕蘸扵庘昄婘锟劯欔橬䔇劆橬麂 NULL 唚䔇臯婪脄䫘㔗認湙啔螷懫膄垹滷垂䯄償 max 認湙䔇蕔镖㔗臙濘懟認䓉臯婺埻滇嘷 state_data_typeinput_data_type 䕩劯䔇施唍欉臘䯄庺準㔗套悩認底䌂傋婉劯嘹媙釂柊冕婔婻麂 NULL 䔇彺哋溇傽潡蔙嘪䫘婔婻麂"strice"䔇䪽攕蘸扵庘昄㔗

套悩䪽攕蘸扵庘昄婉滇婖湚(strict)䔇闼幽垄儖方溇傽婄婘懟婻膷噖臯婪脄䫘幽婫媙釂躻臯崇䊖 NULL 膷噖启 NULL 蘸扵唚認湙儌噕螩蕔镖䔇嘩蔙凹蕔镖婺䔇 NULL 橬垯噘䔇毓彽㔗

套悩橔䂽蘸扵庘昄垔幬婺"strict"闼幽套悩橔䂽䪽攕唚滇 NULL 施儌婉嚔脄䫘垄蔯滇躻媘膷庺婔婻 NULL 䂷悩㔗認欉滇 strict 庘昄䔇溼婩䬹冕㔗婉䞇滇闼䓉愙喕橔䂽崇䊖庘昄埇傖躻䫌锬拷滇劥誫啂 NULL 㔗懫套avg 䔇橔䂽崇䊖庘昄婘镽膷噖螄嘘施儌嚔誫啂 NULL 㔗

臯婺䌂嚚 MINMAX 䔇蕔镖橬施唍埇傖嚻寡婺嘪䫘䘵嚘蔯婉䫘欆柟懟婻膷噖臯㔗套悩認婻蕔镖埇傖套溴嚻寡彍䫘婔婻毐废淉嘩严湺臖垄㔗認麯嘺橸䔇襕挗滇蕔镖媙釂傖淉嘩严嘐亿庺準䔇毐废釺废䫘潊丸婔婻噄䘹扵埖臺臘

SELECT agg(col) FROM tab;

媙釂京庯

SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;

敘崔䔇啺螆滇蕔镖媘䘖 NULL 膷噖幽婫埻橬婘膷噖澇橬麂䷺昄唚䔇施唍垄欉䫘潊 NULL 䂷悩㔗锔婩昄扞䌂傋䔇 < 淉嘩严滇 MIN 䔇锗䫘毐废淉嘩严蔯 >MAX䔇锗䫘淉嘩严㔗臙濘懟鍴麂弄滯䔇淉嘩严滇 B-tree 䘵嚘淉嘩严䌂䔇"償庯"潡蔙"崓庯"亡䘖埙劥彍認䓉嚻寡儖婉嚔䫘昽㔗

埗昄

name

襕录傺䔇蕔镖庘昄劉(埇傖橬昇嚟媞閄)

input_data_type

臖蕔镖庘昄襕崇䊖䔇膷噖昄扞䌂傋㔗襕录傺婔婻镽埗昄蕔镖庘昄埇傖嘪䫘 * 傼敪膷噖昄扞䌂傋彖臘㔗count(*) 儌滇認䓉蕔镖庘昄䔇婔婻垂冋㔗

base_type

婘斓嚟䔇 CREATE AGGREGATE 臺濘婺膷噖昄扞䌂傋滇锔誺 basetype 埗昄毺垔䔇蔯婉滇喍婘蕔镖䔇劉䓄幋劯㔗驔襕濘懟䔇滇認䓉斓嚟臺濘備噕螩婔婻膷噖埗昄㔗襕录傺婔婻镽埗昄蕔镖庘昄埇傖儖 basetype 毺垔婺 "ANY"(蔯婉滇 *)㔗

sfunc

儖婘懟婔婻膷噖臯婪脄䫘䔇䪽攕蘸扵庘昄䔇劉䓄㔗凹庯橬 N 婻埗昄䔇蕔镖庘昄sfunc 媙釂橬 N+1 婻埗昄噽婺䔇丸婔婻埗昄䌂傋婺 state_data_type 噽嘍䔇对陉噾弄滯䔇膷噖昄扞䌂傋㔗庘昄媙釂誫啂婔婻 state_data_type 䌂傋䔇唚㔗認婻庘昄毖埖嘷嬉䪽攕唚启嘷嬉膷噖昄扞幽誫啂婋婻䪽攕唚㔗

state_data_type

蕔镖䔇䪽攕唚䔇昄扞䌂傋

ffunc

婘蘸扵垯欔橬膷噖臯劯脄䫘䔇橔䂽崇䊖庘昄垄螇䞖蕔镖䔇䂷悩㔗溴庘昄媙釂毖埖婔婻䌂傋婺 state_data_type 䔇埗昄㔗蕔镖䔇膷庺昄扞䌂傋赆垔幬婺溴庘昄䔇誫啂䌂傋㔗套悩澇橬弄滯 ffunc 彍嘪䫘蕔镖䂷悩䔇䪽攕唚嘩婺蕔镖䔇䂷悩婫膷庺䌂傋婺 state_data_type

initial_condition

䪽攕唚䔇彺哋螆䘞(唚)㔗垄媙釂滇婔婻 state_data_type 䌂傋埇傖毖埖䔇桺橸婩麟唚㔗套悩澇橬弄滯䪽攕唚彺哋婺 NULL 㔗

sort_operator

䫘庯 MINMAX 䌂傋蕔镖䔇毐废淉嘩严㔗認婻埻滇婔婻淉嘩严劉(埇傖橬昇嚟媞閄)㔗認婻淉嘩严啺螆毖埖启蕔镖婔湙䔇膷噖昄扞䌂傋㔗

CREATE AGGREGATE 䔇埗昄埇傖傖傂嘘釺废幥喍蔯婉埻滇婪麵滆䴺䔇釺废㔗

冋床

埗蓕誗33.10

噚垹攓

CREATE AGGREGATE 滇 PostgreSQL 臺蘔䔇欷匘㔗SQL 湺庖澇橬柊冕䫘潙躻垔幬蕔镖庘昄䔇媘脘㔗

埽蓕

ALTER AGGREGATE, DROP AGGREGATE

劯锔饡釕嬉誕
COPY婪婔亓CREATE CAST