window.out 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065
  1. --
  2. -- WINDOW FUNCTIONS
  3. --
  4. CREATE TEMPORARY TABLE empsalary (
  5. depname varchar,
  6. empno bigint,
  7. salary int,
  8. enroll_date date
  9. );
  10. INSERT INTO empsalary VALUES
  11. ('develop', 10, 5200, '2007-08-01'),
  12. ('sales', 1, 5000, '2006-10-01'),
  13. ('personnel', 5, 3500, '2007-12-10'),
  14. ('sales', 4, 4800, '2007-08-08'),
  15. ('personnel', 2, 3900, '2006-12-23'),
  16. ('develop', 7, 4200, '2008-01-01'),
  17. ('develop', 9, 4500, '2008-01-01'),
  18. ('sales', 3, 4800, '2007-08-01'),
  19. ('develop', 8, 6000, '2006-10-01'),
  20. ('develop', 11, 5200, '2007-08-15');
  21. SELECT depname, empno, salary, sum(salary) OVER (PARTITION BY depname) FROM empsalary ORDER BY depname, salary;
  22. depname | empno | salary | sum
  23. -----------+-------+--------+-------
  24. develop | 7 | 4200 | 25100
  25. develop | 9 | 4500 | 25100
  26. develop | 11 | 5200 | 25100
  27. develop | 10 | 5200 | 25100
  28. develop | 8 | 6000 | 25100
  29. personnel | 5 | 3500 | 7400
  30. personnel | 2 | 3900 | 7400
  31. sales | 3 | 4800 | 14600
  32. sales | 4 | 4800 | 14600
  33. sales | 1 | 5000 | 14600
  34. (10 rows)
  35. SELECT depname, empno, salary, rank() OVER (PARTITION BY depname ORDER BY salary) FROM empsalary;
  36. depname | empno | salary | rank
  37. -----------+-------+--------+------
  38. develop | 7 | 4200 | 1
  39. develop | 9 | 4500 | 2
  40. develop | 11 | 5200 | 3
  41. develop | 10 | 5200 | 3
  42. develop | 8 | 6000 | 5
  43. personnel | 5 | 3500 | 1
  44. personnel | 2 | 3900 | 2
  45. sales | 3 | 4800 | 1
  46. sales | 4 | 4800 | 1
  47. sales | 1 | 5000 | 3
  48. (10 rows)
  49. -- with GROUP BY
  50. SELECT four, ten, SUM(SUM(four)) OVER (PARTITION BY four), AVG(ten) FROM tenk1
  51. GROUP BY four, ten ORDER BY four, ten;
  52. four | ten | sum | avg
  53. ------+-----+------+------------------------
  54. 0 | 0 | 0 | 0.00000000000000000000
  55. 0 | 2 | 0 | 2.0000000000000000
  56. 0 | 4 | 0 | 4.0000000000000000
  57. 0 | 6 | 0 | 6.0000000000000000
  58. 0 | 8 | 0 | 8.0000000000000000
  59. 1 | 1 | 2500 | 1.00000000000000000000
  60. 1 | 3 | 2500 | 3.0000000000000000
  61. 1 | 5 | 2500 | 5.0000000000000000
  62. 1 | 7 | 2500 | 7.0000000000000000
  63. 1 | 9 | 2500 | 9.0000000000000000
  64. 2 | 0 | 5000 | 0.00000000000000000000
  65. 2 | 2 | 5000 | 2.0000000000000000
  66. 2 | 4 | 5000 | 4.0000000000000000
  67. 2 | 6 | 5000 | 6.0000000000000000
  68. 2 | 8 | 5000 | 8.0000000000000000
  69. 3 | 1 | 7500 | 1.00000000000000000000
  70. 3 | 3 | 7500 | 3.0000000000000000
  71. 3 | 5 | 7500 | 5.0000000000000000
  72. 3 | 7 | 7500 | 7.0000000000000000
  73. 3 | 9 | 7500 | 9.0000000000000000
  74. (20 rows)
  75. SELECT depname, empno, salary, sum(salary) OVER w FROM empsalary WINDOW w AS (PARTITION BY depname);
  76. depname | empno | salary | sum
  77. -----------+-------+--------+-------
  78. develop | 11 | 5200 | 25100
  79. develop | 7 | 4200 | 25100
  80. develop | 9 | 4500 | 25100
  81. develop | 8 | 6000 | 25100
  82. develop | 10 | 5200 | 25100
  83. personnel | 5 | 3500 | 7400
  84. personnel | 2 | 3900 | 7400
  85. sales | 3 | 4800 | 14600
  86. sales | 1 | 5000 | 14600
  87. sales | 4 | 4800 | 14600
  88. (10 rows)
  89. SELECT depname, empno, salary, rank() OVER w FROM empsalary WINDOW w AS (PARTITION BY depname ORDER BY salary) ORDER BY rank() OVER w;
  90. depname | empno | salary | rank
  91. -----------+-------+--------+------
  92. develop | 7 | 4200 | 1
  93. personnel | 5 | 3500 | 1
  94. sales | 3 | 4800 | 1
  95. sales | 4 | 4800 | 1
  96. personnel | 2 | 3900 | 2
  97. develop | 9 | 4500 | 2
  98. sales | 1 | 5000 | 3
  99. develop | 11 | 5200 | 3
  100. develop | 10 | 5200 | 3
  101. develop | 8 | 6000 | 5
  102. (10 rows)
  103. -- empty window specification
  104. SELECT COUNT(*) OVER () FROM tenk1 WHERE unique2 < 10;
  105. count
  106. -------
  107. 10
  108. 10
  109. 10
  110. 10
  111. 10
  112. 10
  113. 10
  114. 10
  115. 10
  116. 10
  117. (10 rows)
  118. SELECT COUNT(*) OVER w FROM tenk1 WHERE unique2 < 10 WINDOW w AS ();
  119. count
  120. -------
  121. 10
  122. 10
  123. 10
  124. 10
  125. 10
  126. 10
  127. 10
  128. 10
  129. 10
  130. 10
  131. (10 rows)
  132. -- no window operation
  133. SELECT four FROM tenk1 WHERE FALSE WINDOW w AS (PARTITION BY ten);
  134. four
  135. ------
  136. (0 rows)
  137. -- cumulative aggregate
  138. SELECT sum(four) OVER (PARTITION BY ten ORDER BY unique2) AS sum_1, ten, four FROM tenk1 WHERE unique2 < 10;
  139. sum_1 | ten | four
  140. -------+-----+------
  141. 0 | 0 | 0
  142. 0 | 0 | 0
  143. 2 | 0 | 2
  144. 3 | 1 | 3
  145. 4 | 1 | 1
  146. 5 | 1 | 1
  147. 3 | 3 | 3
  148. 0 | 4 | 0
  149. 1 | 7 | 1
  150. 1 | 9 | 1
  151. (10 rows)
  152. SELECT row_number() OVER (ORDER BY unique2) FROM tenk1 WHERE unique2 < 10;
  153. row_number
  154. ------------
  155. 1
  156. 2
  157. 3
  158. 4
  159. 5
  160. 6
  161. 7
  162. 8
  163. 9
  164. 10
  165. (10 rows)
  166. SELECT rank() OVER (PARTITION BY four ORDER BY ten) AS rank_1, ten, four FROM tenk1 WHERE unique2 < 10;
  167. rank_1 | ten | four
  168. --------+-----+------
  169. 1 | 0 | 0
  170. 1 | 0 | 0
  171. 3 | 4 | 0
  172. 1 | 1 | 1
  173. 1 | 1 | 1
  174. 3 | 7 | 1
  175. 4 | 9 | 1
  176. 1 | 0 | 2
  177. 1 | 1 | 3
  178. 2 | 3 | 3
  179. (10 rows)
  180. SELECT dense_rank() OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
  181. dense_rank | ten | four
  182. ------------+-----+------
  183. 1 | 0 | 0
  184. 1 | 0 | 0
  185. 2 | 4 | 0
  186. 1 | 1 | 1
  187. 1 | 1 | 1
  188. 2 | 7 | 1
  189. 3 | 9 | 1
  190. 1 | 0 | 2
  191. 1 | 1 | 3
  192. 2 | 3 | 3
  193. (10 rows)
  194. SELECT percent_rank() OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
  195. percent_rank | ten | four
  196. --------------------+-----+------
  197. 0 | 0 | 0
  198. 0 | 0 | 0
  199. 1 | 4 | 0
  200. 0 | 1 | 1
  201. 0 | 1 | 1
  202. 0.6666666666666666 | 7 | 1
  203. 1 | 9 | 1
  204. 0 | 0 | 2
  205. 0 | 1 | 3
  206. 1 | 3 | 3
  207. (10 rows)
  208. SELECT cume_dist() OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
  209. cume_dist | ten | four
  210. --------------------+-----+------
  211. 0.6666666666666666 | 0 | 0
  212. 0.6666666666666666 | 0 | 0
  213. 1 | 4 | 0
  214. 0.5 | 1 | 1
  215. 0.5 | 1 | 1
  216. 0.75 | 7 | 1
  217. 1 | 9 | 1
  218. 1 | 0 | 2
  219. 0.5 | 1 | 3
  220. 1 | 3 | 3
  221. (10 rows)
  222. SELECT ntile(3) OVER (ORDER BY ten, four), ten, four FROM tenk1 WHERE unique2 < 10;
  223. ntile | ten | four
  224. -------+-----+------
  225. 1 | 0 | 0
  226. 1 | 0 | 0
  227. 1 | 0 | 2
  228. 1 | 1 | 1
  229. 2 | 1 | 1
  230. 2 | 1 | 3
  231. 2 | 3 | 3
  232. 3 | 4 | 0
  233. 3 | 7 | 1
  234. 3 | 9 | 1
  235. (10 rows)
  236. SELECT ntile(NULL) OVER (ORDER BY ten, four), ten, four FROM tenk1 LIMIT 2;
  237. ntile | ten | four
  238. -------+-----+------
  239. | 0 | 0
  240. | 0 | 0
  241. (2 rows)
  242. SELECT lag(ten) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
  243. lag | ten | four
  244. -----+-----+------
  245. | 0 | 0
  246. 0 | 0 | 0
  247. 0 | 4 | 0
  248. | 1 | 1
  249. 1 | 1 | 1
  250. 1 | 7 | 1
  251. 7 | 9 | 1
  252. | 0 | 2
  253. | 1 | 3
  254. 1 | 3 | 3
  255. (10 rows)
  256. SELECT lag(ten, four) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
  257. lag | ten | four
  258. -----+-----+------
  259. 0 | 0 | 0
  260. 0 | 0 | 0
  261. 4 | 4 | 0
  262. | 1 | 1
  263. 1 | 1 | 1
  264. 1 | 7 | 1
  265. 7 | 9 | 1
  266. | 0 | 2
  267. | 1 | 3
  268. | 3 | 3
  269. (10 rows)
  270. SELECT lag(ten, four, 0) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
  271. lag | ten | four
  272. -----+-----+------
  273. 0 | 0 | 0
  274. 0 | 0 | 0
  275. 4 | 4 | 0
  276. 0 | 1 | 1
  277. 1 | 1 | 1
  278. 1 | 7 | 1
  279. 7 | 9 | 1
  280. 0 | 0 | 2
  281. 0 | 1 | 3
  282. 0 | 3 | 3
  283. (10 rows)
  284. SELECT lag(ten, four, 0.7) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10 ORDER BY four, ten;
  285. lag | ten | four
  286. -----+-----+------
  287. 0 | 0 | 0
  288. 0 | 0 | 0
  289. 4 | 4 | 0
  290. 0.7 | 1 | 1
  291. 1 | 1 | 1
  292. 1 | 7 | 1
  293. 7 | 9 | 1
  294. 0.7 | 0 | 2
  295. 0.7 | 1 | 3
  296. 0.7 | 3 | 3
  297. (10 rows)
  298. SELECT lead(ten) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
  299. lead | ten | four
  300. ------+-----+------
  301. 0 | 0 | 0
  302. 4 | 0 | 0
  303. | 4 | 0
  304. 1 | 1 | 1
  305. 7 | 1 | 1
  306. 9 | 7 | 1
  307. | 9 | 1
  308. | 0 | 2
  309. 3 | 1 | 3
  310. | 3 | 3
  311. (10 rows)
  312. SELECT lead(ten * 2, 1) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
  313. lead | ten | four
  314. ------+-----+------
  315. 0 | 0 | 0
  316. 8 | 0 | 0
  317. | 4 | 0
  318. 2 | 1 | 1
  319. 14 | 1 | 1
  320. 18 | 7 | 1
  321. | 9 | 1
  322. | 0 | 2
  323. 6 | 1 | 3
  324. | 3 | 3
  325. (10 rows)
  326. SELECT lead(ten * 2, 1, -1) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
  327. lead | ten | four
  328. ------+-----+------
  329. 0 | 0 | 0
  330. 8 | 0 | 0
  331. -1 | 4 | 0
  332. 2 | 1 | 1
  333. 14 | 1 | 1
  334. 18 | 7 | 1
  335. -1 | 9 | 1
  336. -1 | 0 | 2
  337. 6 | 1 | 3
  338. -1 | 3 | 3
  339. (10 rows)
  340. SELECT lead(ten * 2, 1, -1.4) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10 ORDER BY four, ten;
  341. lead | ten | four
  342. ------+-----+------
  343. 0 | 0 | 0
  344. 8 | 0 | 0
  345. -1.4 | 4 | 0
  346. 2 | 1 | 1
  347. 14 | 1 | 1
  348. 18 | 7 | 1
  349. -1.4 | 9 | 1
  350. -1.4 | 0 | 2
  351. 6 | 1 | 3
  352. -1.4 | 3 | 3
  353. (10 rows)
  354. SELECT first_value(ten) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
  355. first_value | ten | four
  356. -------------+-----+------
  357. 0 | 0 | 0
  358. 0 | 0 | 0
  359. 0 | 4 | 0
  360. 1 | 1 | 1
  361. 1 | 1 | 1
  362. 1 | 7 | 1
  363. 1 | 9 | 1
  364. 0 | 0 | 2
  365. 1 | 1 | 3
  366. 1 | 3 | 3
  367. (10 rows)
  368. -- last_value returns the last row of the frame, which is CURRENT ROW in ORDER BY window.
  369. SELECT last_value(four) OVER (ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
  370. last_value | ten | four
  371. ------------+-----+------
  372. 0 | 0 | 0
  373. 0 | 0 | 2
  374. 0 | 0 | 0
  375. 1 | 1 | 1
  376. 1 | 1 | 3
  377. 1 | 1 | 1
  378. 3 | 3 | 3
  379. 0 | 4 | 0
  380. 1 | 7 | 1
  381. 1 | 9 | 1
  382. (10 rows)
  383. SELECT last_value(ten) OVER (PARTITION BY four), ten, four FROM
  384. (SELECT * FROM tenk1 WHERE unique2 < 10 ORDER BY four, ten)s
  385. ORDER BY four, ten;
  386. last_value | ten | four
  387. ------------+-----+------
  388. 4 | 0 | 0
  389. 4 | 0 | 0
  390. 4 | 4 | 0
  391. 9 | 1 | 1
  392. 9 | 1 | 1
  393. 9 | 7 | 1
  394. 9 | 9 | 1
  395. 0 | 0 | 2
  396. 3 | 1 | 3
  397. 3 | 3 | 3
  398. (10 rows)
  399. SELECT nth_value(ten, four + 1) OVER (PARTITION BY four), ten, four
  400. FROM (SELECT * FROM tenk1 WHERE unique2 < 10 ORDER BY four, ten)s;
  401. nth_value | ten | four
  402. -----------+-----+------
  403. 0 | 0 | 0
  404. 0 | 0 | 0
  405. 0 | 4 | 0
  406. 1 | 1 | 1
  407. 1 | 1 | 1
  408. 1 | 7 | 1
  409. 1 | 9 | 1
  410. | 0 | 2
  411. | 1 | 3
  412. | 3 | 3
  413. (10 rows)
  414. SELECT ten, two, sum(hundred) AS gsum, sum(sum(hundred)) OVER (PARTITION BY two ORDER BY ten) AS wsum
  415. FROM tenk1 GROUP BY ten, two;
  416. ten | two | gsum | wsum
  417. -----+-----+-------+--------
  418. 0 | 0 | 45000 | 45000
  419. 2 | 0 | 47000 | 92000
  420. 4 | 0 | 49000 | 141000
  421. 6 | 0 | 51000 | 192000
  422. 8 | 0 | 53000 | 245000
  423. 1 | 1 | 46000 | 46000
  424. 3 | 1 | 48000 | 94000
  425. 5 | 1 | 50000 | 144000
  426. 7 | 1 | 52000 | 196000
  427. 9 | 1 | 54000 | 250000
  428. (10 rows)
  429. SELECT count(*) OVER (PARTITION BY four), four FROM (SELECT * FROM tenk1 WHERE two = 1)s WHERE unique2 < 10;
  430. count | four
  431. -------+------
  432. 4 | 1
  433. 4 | 1
  434. 4 | 1
  435. 4 | 1
  436. 2 | 3
  437. 2 | 3
  438. (6 rows)
  439. SELECT (count(*) OVER (PARTITION BY four ORDER BY ten) +
  440. sum(hundred) OVER (PARTITION BY four ORDER BY ten))::varchar AS cntsum
  441. FROM tenk1 WHERE unique2 < 10;
  442. cntsum
  443. --------
  444. 22
  445. 22
  446. 87
  447. 24
  448. 24
  449. 82
  450. 92
  451. 51
  452. 92
  453. 136
  454. (10 rows)
  455. -- opexpr with different windows evaluation.
  456. SELECT * FROM(
  457. SELECT count(*) OVER (PARTITION BY four ORDER BY ten) +
  458. sum(hundred) OVER (PARTITION BY two ORDER BY ten) AS total,
  459. count(*) OVER (PARTITION BY four ORDER BY ten) AS fourcount,
  460. sum(hundred) OVER (PARTITION BY two ORDER BY ten) AS twosum
  461. FROM tenk1
  462. )sub
  463. WHERE total <> fourcount + twosum;
  464. total | fourcount | twosum
  465. -------+-----------+--------
  466. (0 rows)
  467. SELECT avg(four) OVER (PARTITION BY four ORDER BY thousand / 100) FROM tenk1 WHERE unique2 < 10;
  468. avg
  469. ------------------------
  470. 0.00000000000000000000
  471. 0.00000000000000000000
  472. 0.00000000000000000000
  473. 1.00000000000000000000
  474. 1.00000000000000000000
  475. 1.00000000000000000000
  476. 1.00000000000000000000
  477. 2.0000000000000000
  478. 3.0000000000000000
  479. 3.0000000000000000
  480. (10 rows)
  481. SELECT ten, two, sum(hundred) AS gsum, sum(sum(hundred)) OVER win AS wsum
  482. FROM tenk1 GROUP BY ten, two WINDOW win AS (PARTITION BY two ORDER BY ten);
  483. ten | two | gsum | wsum
  484. -----+-----+-------+--------
  485. 0 | 0 | 45000 | 45000
  486. 2 | 0 | 47000 | 92000
  487. 4 | 0 | 49000 | 141000
  488. 6 | 0 | 51000 | 192000
  489. 8 | 0 | 53000 | 245000
  490. 1 | 1 | 46000 | 46000
  491. 3 | 1 | 48000 | 94000
  492. 5 | 1 | 50000 | 144000
  493. 7 | 1 | 52000 | 196000
  494. 9 | 1 | 54000 | 250000
  495. (10 rows)
  496. -- more than one window with GROUP BY
  497. SELECT sum(salary),
  498. row_number() OVER (ORDER BY depname),
  499. sum(sum(salary)) OVER (ORDER BY depname DESC)
  500. FROM empsalary GROUP BY depname;
  501. sum | row_number | sum
  502. -------+------------+-------
  503. 25100 | 1 | 47100
  504. 7400 | 2 | 22000
  505. 14600 | 3 | 14600
  506. (3 rows)
  507. -- identical windows with different names
  508. SELECT sum(salary) OVER w1, count(*) OVER w2
  509. FROM empsalary WINDOW w1 AS (ORDER BY salary), w2 AS (ORDER BY salary);
  510. sum | count
  511. -------+-------
  512. 3500 | 1
  513. 7400 | 2
  514. 11600 | 3
  515. 16100 | 4
  516. 25700 | 6
  517. 25700 | 6
  518. 30700 | 7
  519. 41100 | 9
  520. 41100 | 9
  521. 47100 | 10
  522. (10 rows)
  523. -- subplan
  524. SELECT lead(ten, (SELECT two FROM tenk1 WHERE s.unique2 = unique2)) OVER (PARTITION BY four ORDER BY ten)
  525. FROM tenk1 s WHERE unique2 < 10;
  526. lead
  527. ------
  528. 0
  529. 0
  530. 4
  531. 1
  532. 7
  533. 9
  534. 0
  535. 3
  536. (10 rows)
  537. -- empty table
  538. SELECT count(*) OVER (PARTITION BY four) FROM (SELECT * FROM tenk1 WHERE FALSE)s;
  539. count
  540. -------
  541. (0 rows)
  542. -- mixture of agg/wfunc in the same window
  543. SELECT sum(salary) OVER w, rank() OVER w FROM empsalary WINDOW w AS (PARTITION BY depname ORDER BY salary DESC);
  544. sum | rank
  545. -------+------
  546. 6000 | 1
  547. 16400 | 2
  548. 16400 | 2
  549. 20900 | 4
  550. 25100 | 5
  551. 3900 | 1
  552. 7400 | 2
  553. 5000 | 1
  554. 14600 | 2
  555. 14600 | 2
  556. (10 rows)
  557. -- strict aggs
  558. SELECT empno, depname, salary, bonus, depadj, MIN(bonus) OVER (ORDER BY empno), MAX(depadj) OVER () FROM(
  559. SELECT *,
  560. CASE WHEN enroll_date < '2008-01-01' THEN 2008 - extract(YEAR FROM enroll_date) END * 500 AS bonus,
  561. CASE WHEN
  562. AVG(salary) OVER (PARTITION BY depname) < salary
  563. THEN 200 END AS depadj FROM empsalary
  564. )s;
  565. empno | depname | salary | bonus | depadj | min | max
  566. -------+-----------+--------+-------+--------+------+-----
  567. 1 | sales | 5000 | 1000 | 200 | 1000 | 200
  568. 2 | personnel | 3900 | 1000 | 200 | 1000 | 200
  569. 3 | sales | 4800 | 500 | | 500 | 200
  570. 4 | sales | 4800 | 500 | | 500 | 200
  571. 5 | personnel | 3500 | 500 | | 500 | 200
  572. 7 | develop | 4200 | | | 500 | 200
  573. 8 | develop | 6000 | 1000 | 200 | 500 | 200
  574. 9 | develop | 4500 | | | 500 | 200
  575. 10 | develop | 5200 | 500 | 200 | 500 | 200
  576. 11 | develop | 5200 | 500 | 200 | 500 | 200
  577. (10 rows)
  578. -- window function over ungrouped agg over empty row set (bug before 9.1)
  579. SELECT SUM(COUNT(f1)) OVER () FROM int4_tbl WHERE f1=42;
  580. sum
  581. -----
  582. 0
  583. (1 row)
  584. -- window function with ORDER BY an expression involving aggregates (9.1 bug)
  585. select ten,
  586. sum(unique1) + sum(unique2) as res,
  587. rank() over (order by sum(unique1) + sum(unique2)) as rank
  588. from tenk1
  589. group by ten order by ten;
  590. ten | res | rank
  591. -----+----------+------
  592. 0 | 9976146 | 4
  593. 1 | 10114187 | 9
  594. 2 | 10059554 | 8
  595. 3 | 9878541 | 1
  596. 4 | 9881005 | 2
  597. 5 | 9981670 | 5
  598. 6 | 9947099 | 3
  599. 7 | 10120309 | 10
  600. 8 | 9991305 | 6
  601. 9 | 10040184 | 7
  602. (10 rows)
  603. -- window and aggregate with GROUP BY expression (9.2 bug)
  604. explain (costs off)
  605. select first_value(max(x)) over (), y
  606. from (select unique1 as x, ten+four as y from tenk1) ss
  607. group by y;
  608. QUERY PLAN
  609. ---------------------------------------------
  610. WindowAgg
  611. -> HashAggregate
  612. Group Key: (tenk1.ten + tenk1.four)
  613. -> Seq Scan on tenk1
  614. (4 rows)
  615. -- test non-default frame specifications
  616. SELECT four, ten,
  617. sum(ten) over (partition by four order by ten),
  618. last_value(ten) over (partition by four order by ten)
  619. FROM (select distinct ten, four from tenk1) ss;
  620. four | ten | sum | last_value
  621. ------+-----+-----+------------
  622. 0 | 0 | 0 | 0
  623. 0 | 2 | 2 | 2
  624. 0 | 4 | 6 | 4
  625. 0 | 6 | 12 | 6
  626. 0 | 8 | 20 | 8
  627. 1 | 1 | 1 | 1
  628. 1 | 3 | 4 | 3
  629. 1 | 5 | 9 | 5
  630. 1 | 7 | 16 | 7
  631. 1 | 9 | 25 | 9
  632. 2 | 0 | 0 | 0
  633. 2 | 2 | 2 | 2
  634. 2 | 4 | 6 | 4
  635. 2 | 6 | 12 | 6
  636. 2 | 8 | 20 | 8
  637. 3 | 1 | 1 | 1
  638. 3 | 3 | 4 | 3
  639. 3 | 5 | 9 | 5
  640. 3 | 7 | 16 | 7
  641. 3 | 9 | 25 | 9
  642. (20 rows)
  643. SELECT four, ten,
  644. sum(ten) over (partition by four order by ten range between unbounded preceding and current row),
  645. last_value(ten) over (partition by four order by ten range between unbounded preceding and current row)
  646. FROM (select distinct ten, four from tenk1) ss;
  647. four | ten | sum | last_value
  648. ------+-----+-----+------------
  649. 0 | 0 | 0 | 0
  650. 0 | 2 | 2 | 2
  651. 0 | 4 | 6 | 4
  652. 0 | 6 | 12 | 6
  653. 0 | 8 | 20 | 8
  654. 1 | 1 | 1 | 1
  655. 1 | 3 | 4 | 3
  656. 1 | 5 | 9 | 5
  657. 1 | 7 | 16 | 7
  658. 1 | 9 | 25 | 9
  659. 2 | 0 | 0 | 0
  660. 2 | 2 | 2 | 2
  661. 2 | 4 | 6 | 4
  662. 2 | 6 | 12 | 6
  663. 2 | 8 | 20 | 8
  664. 3 | 1 | 1 | 1
  665. 3 | 3 | 4 | 3
  666. 3 | 5 | 9 | 5
  667. 3 | 7 | 16 | 7
  668. 3 | 9 | 25 | 9
  669. (20 rows)
  670. SELECT four, ten,
  671. sum(ten) over (partition by four order by ten range between unbounded preceding and unbounded following),
  672. last_value(ten) over (partition by four order by ten range between unbounded preceding and unbounded following)
  673. FROM (select distinct ten, four from tenk1) ss;
  674. four | ten | sum | last_value
  675. ------+-----+-----+------------
  676. 0 | 0 | 20 | 8
  677. 0 | 2 | 20 | 8
  678. 0 | 4 | 20 | 8
  679. 0 | 6 | 20 | 8
  680. 0 | 8 | 20 | 8
  681. 1 | 1 | 25 | 9
  682. 1 | 3 | 25 | 9
  683. 1 | 5 | 25 | 9
  684. 1 | 7 | 25 | 9
  685. 1 | 9 | 25 | 9
  686. 2 | 0 | 20 | 8
  687. 2 | 2 | 20 | 8
  688. 2 | 4 | 20 | 8
  689. 2 | 6 | 20 | 8
  690. 2 | 8 | 20 | 8
  691. 3 | 1 | 25 | 9
  692. 3 | 3 | 25 | 9
  693. 3 | 5 | 25 | 9
  694. 3 | 7 | 25 | 9
  695. 3 | 9 | 25 | 9
  696. (20 rows)
  697. SELECT four, ten/4 as two,
  698. sum(ten/4) over (partition by four order by ten/4 range between unbounded preceding and current row),
  699. last_value(ten/4) over (partition by four order by ten/4 range between unbounded preceding and current row)
  700. FROM (select distinct ten, four from tenk1) ss;
  701. four | two | sum | last_value
  702. ------+-----+-----+------------
  703. 0 | 0 | 0 | 0
  704. 0 | 0 | 0 | 0
  705. 0 | 1 | 2 | 1
  706. 0 | 1 | 2 | 1
  707. 0 | 2 | 4 | 2
  708. 1 | 0 | 0 | 0
  709. 1 | 0 | 0 | 0
  710. 1 | 1 | 2 | 1
  711. 1 | 1 | 2 | 1
  712. 1 | 2 | 4 | 2
  713. 2 | 0 | 0 | 0
  714. 2 | 0 | 0 | 0
  715. 2 | 1 | 2 | 1
  716. 2 | 1 | 2 | 1
  717. 2 | 2 | 4 | 2
  718. 3 | 0 | 0 | 0
  719. 3 | 0 | 0 | 0
  720. 3 | 1 | 2 | 1
  721. 3 | 1 | 2 | 1
  722. 3 | 2 | 4 | 2
  723. (20 rows)
  724. SELECT four, ten/4 as two,
  725. sum(ten/4) over (partition by four order by ten/4 rows between unbounded preceding and current row),
  726. last_value(ten/4) over (partition by four order by ten/4 rows between unbounded preceding and current row)
  727. FROM (select distinct ten, four from tenk1) ss;
  728. four | two | sum | last_value
  729. ------+-----+-----+------------
  730. 0 | 0 | 0 | 0
  731. 0 | 0 | 0 | 0
  732. 0 | 1 | 1 | 1
  733. 0 | 1 | 2 | 1
  734. 0 | 2 | 4 | 2
  735. 1 | 0 | 0 | 0
  736. 1 | 0 | 0 | 0
  737. 1 | 1 | 1 | 1
  738. 1 | 1 | 2 | 1
  739. 1 | 2 | 4 | 2
  740. 2 | 0 | 0 | 0
  741. 2 | 0 | 0 | 0
  742. 2 | 1 | 1 | 1
  743. 2 | 1 | 2 | 1
  744. 2 | 2 | 4 | 2
  745. 3 | 0 | 0 | 0
  746. 3 | 0 | 0 | 0
  747. 3 | 1 | 1 | 1
  748. 3 | 1 | 2 | 1
  749. 3 | 2 | 4 | 2
  750. (20 rows)
  751. SELECT sum(unique1) over (order by four range between current row and unbounded following),
  752. unique1, four
  753. FROM tenk1 WHERE unique1 < 10;
  754. sum | unique1 | four
  755. -----+---------+------
  756. 45 | 0 | 0
  757. 45 | 8 | 0
  758. 45 | 4 | 0
  759. 33 | 5 | 1
  760. 33 | 9 | 1
  761. 33 | 1 | 1
  762. 18 | 6 | 2
  763. 18 | 2 | 2
  764. 10 | 3 | 3
  765. 10 | 7 | 3
  766. (10 rows)
  767. SELECT sum(unique1) over (rows between current row and unbounded following),
  768. unique1, four
  769. FROM tenk1 WHERE unique1 < 10;
  770. sum | unique1 | four
  771. -----+---------+------
  772. 45 | 4 | 0
  773. 41 | 2 | 2
  774. 39 | 1 | 1
  775. 38 | 6 | 2
  776. 32 | 9 | 1
  777. 23 | 8 | 0
  778. 15 | 5 | 1
  779. 10 | 3 | 3
  780. 7 | 7 | 3
  781. 0 | 0 | 0
  782. (10 rows)
  783. SELECT sum(unique1) over (rows between 2 preceding and 2 following),
  784. unique1, four
  785. FROM tenk1 WHERE unique1 < 10;
  786. sum | unique1 | four
  787. -----+---------+------
  788. 7 | 4 | 0
  789. 13 | 2 | 2
  790. 22 | 1 | 1
  791. 26 | 6 | 2
  792. 29 | 9 | 1
  793. 31 | 8 | 0
  794. 32 | 5 | 1
  795. 23 | 3 | 3
  796. 15 | 7 | 3
  797. 10 | 0 | 0
  798. (10 rows)
  799. SELECT sum(unique1) over (rows between 2 preceding and 2 following exclude no others),
  800. unique1, four
  801. FROM tenk1 WHERE unique1 < 10;
  802. sum | unique1 | four
  803. -----+---------+------
  804. 7 | 4 | 0
  805. 13 | 2 | 2
  806. 22 | 1 | 1
  807. 26 | 6 | 2
  808. 29 | 9 | 1
  809. 31 | 8 | 0
  810. 32 | 5 | 1
  811. 23 | 3 | 3
  812. 15 | 7 | 3
  813. 10 | 0 | 0
  814. (10 rows)
  815. SELECT sum(unique1) over (rows between 2 preceding and 2 following exclude current row),
  816. unique1, four
  817. FROM tenk1 WHERE unique1 < 10;
  818. sum | unique1 | four
  819. -----+---------+------
  820. 3 | 4 | 0
  821. 11 | 2 | 2
  822. 21 | 1 | 1
  823. 20 | 6 | 2
  824. 20 | 9 | 1
  825. 23 | 8 | 0
  826. 27 | 5 | 1
  827. 20 | 3 | 3
  828. 8 | 7 | 3
  829. 10 | 0 | 0
  830. (10 rows)
  831. SELECT sum(unique1) over (rows between 2 preceding and 2 following exclude group),
  832. unique1, four
  833. FROM tenk1 WHERE unique1 < 10;
  834. sum | unique1 | four
  835. -----+---------+------
  836. | 4 | 0
  837. | 2 | 2
  838. | 1 | 1
  839. | 6 | 2
  840. | 9 | 1
  841. | 8 | 0
  842. | 5 | 1
  843. | 3 | 3
  844. | 7 | 3
  845. | 0 | 0
  846. (10 rows)
  847. SELECT sum(unique1) over (rows between 2 preceding and 2 following exclude ties),
  848. unique1, four
  849. FROM tenk1 WHERE unique1 < 10;
  850. sum | unique1 | four
  851. -----+---------+------
  852. 4 | 4 | 0
  853. 2 | 2 | 2
  854. 1 | 1 | 1
  855. 6 | 6 | 2
  856. 9 | 9 | 1
  857. 8 | 8 | 0
  858. 5 | 5 | 1
  859. 3 | 3 | 3
  860. 7 | 7 | 3
  861. 0 | 0 | 0
  862. (10 rows)
  863. SELECT first_value(unique1) over (ORDER BY four rows between current row and 2 following exclude current row),
  864. unique1, four
  865. FROM tenk1 WHERE unique1 < 10;
  866. first_value | unique1 | four
  867. -------------+---------+------
  868. 8 | 0 | 0
  869. 4 | 8 | 0
  870. 5 | 4 | 0
  871. 9 | 5 | 1
  872. 1 | 9 | 1
  873. 6 | 1 | 1
  874. 2 | 6 | 2
  875. 3 | 2 | 2
  876. 7 | 3 | 3
  877. | 7 | 3
  878. (10 rows)
  879. SELECT first_value(unique1) over (ORDER BY four rows between current row and 2 following exclude group),
  880. unique1, four
  881. FROM tenk1 WHERE unique1 < 10;
  882. first_value | unique1 | four
  883. -------------+---------+------
  884. | 0 | 0
  885. 5 | 8 | 0
  886. 5 | 4 | 0
  887. | 5 | 1
  888. 6 | 9 | 1
  889. 6 | 1 | 1
  890. 3 | 6 | 2
  891. 3 | 2 | 2
  892. | 3 | 3
  893. | 7 | 3
  894. (10 rows)
  895. SELECT first_value(unique1) over (ORDER BY four rows between current row and 2 following exclude ties),
  896. unique1, four
  897. FROM tenk1 WHERE unique1 < 10;
  898. first_value | unique1 | four
  899. -------------+---------+------
  900. 0 | 0 | 0
  901. 8 | 8 | 0
  902. 4 | 4 | 0
  903. 5 | 5 | 1
  904. 9 | 9 | 1
  905. 1 | 1 | 1
  906. 6 | 6 | 2
  907. 2 | 2 | 2
  908. 3 | 3 | 3
  909. 7 | 7 | 3
  910. (10 rows)
  911. SELECT last_value(unique1) over (ORDER BY four rows between current row and 2 following exclude current row),
  912. unique1, four
  913. FROM tenk1 WHERE unique1 < 10;
  914. last_value | unique1 | four
  915. ------------+---------+------
  916. 4 | 0 | 0
  917. 5 | 8 | 0
  918. 9 | 4 | 0
  919. 1 | 5 | 1
  920. 6 | 9 | 1
  921. 2 | 1 | 1
  922. 3 | 6 | 2
  923. 7 | 2 | 2
  924. 7 | 3 | 3
  925. | 7 | 3
  926. (10 rows)
  927. SELECT last_value(unique1) over (ORDER BY four rows between current row and 2 following exclude group),
  928. unique1, four
  929. FROM tenk1 WHERE unique1 < 10;
  930. last_value | unique1 | four
  931. ------------+---------+------
  932. | 0 | 0
  933. 5 | 8 | 0
  934. 9 | 4 | 0
  935. | 5 | 1
  936. 6 | 9 | 1
  937. 2 | 1 | 1
  938. 3 | 6 | 2
  939. 7 | 2 | 2
  940. | 3 | 3
  941. | 7 | 3
  942. (10 rows)
  943. SELECT last_value(unique1) over (ORDER BY four rows between current row and 2 following exclude ties),
  944. unique1, four
  945. FROM tenk1 WHERE unique1 < 10;
  946. last_value | unique1 | four
  947. ------------+---------+------
  948. 0 | 0 | 0
  949. 5 | 8 | 0
  950. 9 | 4 | 0
  951. 5 | 5 | 1
  952. 6 | 9 | 1
  953. 2 | 1 | 1
  954. 3 | 6 | 2
  955. 7 | 2 | 2
  956. 3 | 3 | 3
  957. 7 | 7 | 3
  958. (10 rows)
  959. SELECT sum(unique1) over (rows between 2 preceding and 1 preceding),
  960. unique1, four
  961. FROM tenk1 WHERE unique1 < 10;
  962. sum | unique1 | four
  963. -----+---------+------
  964. | 4 | 0
  965. 4 | 2 | 2
  966. 6 | 1 | 1
  967. 3 | 6 | 2
  968. 7 | 9 | 1
  969. 15 | 8 | 0
  970. 17 | 5 | 1
  971. 13 | 3 | 3
  972. 8 | 7 | 3
  973. 10 | 0 | 0
  974. (10 rows)
  975. SELECT sum(unique1) over (rows between 1 following and 3 following),
  976. unique1, four
  977. FROM tenk1 WHERE unique1 < 10;
  978. sum | unique1 | four
  979. -----+---------+------
  980. 9 | 4 | 0
  981. 16 | 2 | 2
  982. 23 | 1 | 1
  983. 22 | 6 | 2
  984. 16 | 9 | 1
  985. 15 | 8 | 0
  986. 10 | 5 | 1
  987. 7 | 3 | 3
  988. 0 | 7 | 3
  989. | 0 | 0
  990. (10 rows)
  991. SELECT sum(unique1) over (rows between unbounded preceding and 1 following),
  992. unique1, four
  993. FROM tenk1 WHERE unique1 < 10;
  994. sum | unique1 | four
  995. -----+---------+------
  996. 6 | 4 | 0
  997. 7 | 2 | 2
  998. 13 | 1 | 1
  999. 22 | 6 | 2
  1000. 30 | 9 | 1
  1001. 35 | 8 | 0
  1002. 38 | 5 | 1
  1003. 45 | 3 | 3
  1004. 45 | 7 | 3
  1005. 45 | 0 | 0
  1006. (10 rows)
  1007. SELECT sum(unique1) over (w range between current row and unbounded following),
  1008. unique1, four
  1009. FROM tenk1 WHERE unique1 < 10 WINDOW w AS (order by four);
  1010. sum | unique1 | four
  1011. -----+---------+------
  1012. 45 | 0 | 0
  1013. 45 | 8 | 0
  1014. 45 | 4 | 0
  1015. 33 | 5 | 1
  1016. 33 | 9 | 1
  1017. 33 | 1 | 1
  1018. 18 | 6 | 2
  1019. 18 | 2 | 2
  1020. 10 | 3 | 3
  1021. 10 | 7 | 3
  1022. (10 rows)
  1023. SELECT sum(unique1) over (w range between unbounded preceding and current row exclude current row),
  1024. unique1, four
  1025. FROM tenk1 WHERE unique1 < 10 WINDOW w AS (order by four);
  1026. sum | unique1 | four
  1027. -----+---------+------
  1028. 12 | 0 | 0
  1029. 4 | 8 | 0
  1030. 8 | 4 | 0
  1031. 22 | 5 | 1
  1032. 18 | 9 | 1
  1033. 26 | 1 | 1
  1034. 29 | 6 | 2
  1035. 33 | 2 | 2
  1036. 42 | 3 | 3
  1037. 38 | 7 | 3
  1038. (10 rows)
  1039. SELECT sum(unique1) over (w range between unbounded preceding and current row exclude group),
  1040. unique1, four
  1041. FROM tenk1 WHERE unique1 < 10 WINDOW w AS (order by four);
  1042. sum | unique1 | four
  1043. -----+---------+------
  1044. | 0 | 0
  1045. | 8 | 0
  1046. | 4 | 0
  1047. 12 | 5 | 1
  1048. 12 | 9 | 1
  1049. 12 | 1 | 1
  1050. 27 | 6 | 2
  1051. 27 | 2 | 2
  1052. 35 | 3 | 3
  1053. 35 | 7 | 3
  1054. (10 rows)
  1055. SELECT sum(unique1) over (w range between unbounded preceding and current row exclude ties),
  1056. unique1, four
  1057. FROM tenk1 WHERE unique1 < 10 WINDOW w AS (order by four);
  1058. sum | unique1 | four
  1059. -----+---------+------
  1060. 0 | 0 | 0
  1061. 8 | 8 | 0
  1062. 4 | 4 | 0
  1063. 17 | 5 | 1
  1064. 21 | 9 | 1
  1065. 13 | 1 | 1
  1066. 33 | 6 | 2
  1067. 29 | 2 | 2
  1068. 38 | 3 | 3
  1069. 42 | 7 | 3
  1070. (10 rows)
  1071. SELECT first_value(unique1) over w,
  1072. nth_value(unique1, 2) over w AS nth_2,
  1073. last_value(unique1) over w, unique1, four
  1074. FROM tenk1 WHERE unique1 < 10
  1075. WINDOW w AS (order by four range between current row and unbounded following);
  1076. first_value | nth_2 | last_value | unique1 | four
  1077. -------------+-------+------------+---------+------
  1078. 0 | 8 | 7 | 0 | 0
  1079. 0 | 8 | 7 | 8 | 0
  1080. 0 | 8 | 7 | 4 | 0
  1081. 5 | 9 | 7 | 5 | 1
  1082. 5 | 9 | 7 | 9 | 1
  1083. 5 | 9 | 7 | 1 | 1
  1084. 6 | 2 | 7 | 6 | 2
  1085. 6 | 2 | 7 | 2 | 2
  1086. 3 | 7 | 7 | 3 | 3
  1087. 3 | 7 | 7 | 7 | 3
  1088. (10 rows)
  1089. SELECT sum(unique1) over
  1090. (order by unique1
  1091. rows (SELECT unique1 FROM tenk1 ORDER BY unique1 LIMIT 1) + 1 PRECEDING),
  1092. unique1
  1093. FROM tenk1 WHERE unique1 < 10;
  1094. sum | unique1
  1095. -----+---------
  1096. 0 | 0
  1097. 1 | 1
  1098. 3 | 2
  1099. 5 | 3
  1100. 7 | 4
  1101. 9 | 5
  1102. 11 | 6
  1103. 13 | 7
  1104. 15 | 8
  1105. 17 | 9
  1106. (10 rows)
  1107. CREATE TEMP VIEW v_window AS
  1108. SELECT i, sum(i) over (order by i rows between 1 preceding and 1 following) as sum_rows
  1109. FROM generate_series(1, 10) i;
  1110. SELECT * FROM v_window;
  1111. i | sum_rows
  1112. ----+----------
  1113. 1 | 3
  1114. 2 | 6
  1115. 3 | 9
  1116. 4 | 12
  1117. 5 | 15
  1118. 6 | 18
  1119. 7 | 21
  1120. 8 | 24
  1121. 9 | 27
  1122. 10 | 19
  1123. (10 rows)
  1124. SELECT pg_get_viewdef('v_window');
  1125. pg_get_viewdef
  1126. ---------------------------------------------------------------------------------------
  1127. SELECT i.i, +
  1128. sum(i.i) OVER (ORDER BY i.i ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS sum_rows+
  1129. FROM generate_series(1, 10) i(i);
  1130. (1 row)
  1131. CREATE OR REPLACE TEMP VIEW v_window AS
  1132. SELECT i, sum(i) over (order by i rows between 1 preceding and 1 following
  1133. exclude current row) as sum_rows FROM generate_series(1, 10) i;
  1134. SELECT * FROM v_window;
  1135. i | sum_rows
  1136. ----+----------
  1137. 1 | 2
  1138. 2 | 4
  1139. 3 | 6
  1140. 4 | 8
  1141. 5 | 10
  1142. 6 | 12
  1143. 7 | 14
  1144. 8 | 16
  1145. 9 | 18
  1146. 10 | 9
  1147. (10 rows)
  1148. SELECT pg_get_viewdef('v_window');
  1149. pg_get_viewdef
  1150. -----------------------------------------------------------------------------------------------------------
  1151. SELECT i.i, +
  1152. sum(i.i) OVER (ORDER BY i.i ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING EXCLUDE CURRENT ROW) AS sum_rows+
  1153. FROM generate_series(1, 10) i(i);
  1154. (1 row)
  1155. CREATE OR REPLACE TEMP VIEW v_window AS
  1156. SELECT i, sum(i) over (order by i rows between 1 preceding and 1 following
  1157. exclude group) as sum_rows FROM generate_series(1, 10) i;
  1158. SELECT * FROM v_window;
  1159. i | sum_rows
  1160. ----+----------
  1161. 1 | 2
  1162. 2 | 4
  1163. 3 | 6
  1164. 4 | 8
  1165. 5 | 10
  1166. 6 | 12
  1167. 7 | 14
  1168. 8 | 16
  1169. 9 | 18
  1170. 10 | 9
  1171. (10 rows)
  1172. SELECT pg_get_viewdef('v_window');
  1173. pg_get_viewdef
  1174. -----------------------------------------------------------------------------------------------------
  1175. SELECT i.i, +
  1176. sum(i.i) OVER (ORDER BY i.i ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING EXCLUDE GROUP) AS sum_rows+
  1177. FROM generate_series(1, 10) i(i);
  1178. (1 row)
  1179. CREATE OR REPLACE TEMP VIEW v_window AS
  1180. SELECT i, sum(i) over (order by i rows between 1 preceding and 1 following
  1181. exclude ties) as sum_rows FROM generate_series(1, 10) i;
  1182. SELECT * FROM v_window;
  1183. i | sum_rows
  1184. ----+----------
  1185. 1 | 3
  1186. 2 | 6
  1187. 3 | 9
  1188. 4 | 12
  1189. 5 | 15
  1190. 6 | 18
  1191. 7 | 21
  1192. 8 | 24
  1193. 9 | 27
  1194. 10 | 19
  1195. (10 rows)
  1196. SELECT pg_get_viewdef('v_window');
  1197. pg_get_viewdef
  1198. ----------------------------------------------------------------------------------------------------
  1199. SELECT i.i, +
  1200. sum(i.i) OVER (ORDER BY i.i ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING EXCLUDE TIES) AS sum_rows+
  1201. FROM generate_series(1, 10) i(i);
  1202. (1 row)
  1203. CREATE OR REPLACE TEMP VIEW v_window AS
  1204. SELECT i, sum(i) over (order by i rows between 1 preceding and 1 following
  1205. exclude no others) as sum_rows FROM generate_series(1, 10) i;
  1206. SELECT * FROM v_window;
  1207. i | sum_rows
  1208. ----+----------
  1209. 1 | 3
  1210. 2 | 6
  1211. 3 | 9
  1212. 4 | 12
  1213. 5 | 15
  1214. 6 | 18
  1215. 7 | 21
  1216. 8 | 24
  1217. 9 | 27
  1218. 10 | 19
  1219. (10 rows)
  1220. SELECT pg_get_viewdef('v_window');
  1221. pg_get_viewdef
  1222. ---------------------------------------------------------------------------------------
  1223. SELECT i.i, +
  1224. sum(i.i) OVER (ORDER BY i.i ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS sum_rows+
  1225. FROM generate_series(1, 10) i(i);
  1226. (1 row)
  1227. CREATE OR REPLACE TEMP VIEW v_window AS
  1228. SELECT i, sum(i) over (order by i groups between 1 preceding and 1 following) as sum_rows FROM generate_series(1, 10) i;
  1229. SELECT * FROM v_window;
  1230. i | sum_rows
  1231. ----+----------
  1232. 1 | 3
  1233. 2 | 6
  1234. 3 | 9
  1235. 4 | 12
  1236. 5 | 15
  1237. 6 | 18
  1238. 7 | 21
  1239. 8 | 24
  1240. 9 | 27
  1241. 10 | 19
  1242. (10 rows)
  1243. SELECT pg_get_viewdef('v_window');
  1244. pg_get_viewdef
  1245. -----------------------------------------------------------------------------------------
  1246. SELECT i.i, +
  1247. sum(i.i) OVER (ORDER BY i.i GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS sum_rows+
  1248. FROM generate_series(1, 10) i(i);
  1249. (1 row)
  1250. DROP VIEW v_window;
  1251. CREATE TEMP VIEW v_window AS
  1252. SELECT i, min(i) over (order by i range between '1 day' preceding and '10 days' following) as min_i
  1253. FROM generate_series(now(), now()+'100 days'::interval, '1 hour') i;
  1254. SELECT pg_get_viewdef('v_window');
  1255. pg_get_viewdef
  1256. ---------------------------------------------------------------------------------------------------------------------------
  1257. SELECT i.i, +
  1258. min(i.i) OVER (ORDER BY i.i RANGE BETWEEN '@ 1 day'::interval PRECEDING AND '@ 10 days'::interval FOLLOWING) AS min_i+
  1259. FROM generate_series(now(), (now() + '@ 100 days'::interval), '@ 1 hour'::interval) i(i);
  1260. (1 row)
  1261. -- RANGE offset PRECEDING/FOLLOWING tests
  1262. SELECT sum(unique1) over (order by four range between 2::int8 preceding and 1::int2 preceding),
  1263. unique1, four
  1264. FROM tenk1 WHERE unique1 < 10;
  1265. sum | unique1 | four
  1266. -----+---------+------
  1267. | 0 | 0
  1268. | 8 | 0
  1269. | 4 | 0
  1270. 12 | 5 | 1
  1271. 12 | 9 | 1
  1272. 12 | 1 | 1
  1273. 27 | 6 | 2
  1274. 27 | 2 | 2
  1275. 23 | 3 | 3
  1276. 23 | 7 | 3
  1277. (10 rows)
  1278. SELECT sum(unique1) over (order by four desc range between 2::int8 preceding and 1::int2 preceding),
  1279. unique1, four
  1280. FROM tenk1 WHERE unique1 < 10;
  1281. sum | unique1 | four
  1282. -----+---------+------
  1283. | 3 | 3
  1284. | 7 | 3
  1285. 10 | 6 | 2
  1286. 10 | 2 | 2
  1287. 18 | 9 | 1
  1288. 18 | 5 | 1
  1289. 18 | 1 | 1
  1290. 23 | 0 | 0
  1291. 23 | 8 | 0
  1292. 23 | 4 | 0
  1293. (10 rows)
  1294. SELECT sum(unique1) over (order by four range between 2::int8 preceding and 1::int2 preceding exclude no others),
  1295. unique1, four
  1296. FROM tenk1 WHERE unique1 < 10;
  1297. sum | unique1 | four
  1298. -----+---------+------
  1299. | 0 | 0
  1300. | 8 | 0
  1301. | 4 | 0
  1302. 12 | 5 | 1
  1303. 12 | 9 | 1
  1304. 12 | 1 | 1
  1305. 27 | 6 | 2
  1306. 27 | 2 | 2
  1307. 23 | 3 | 3
  1308. 23 | 7 | 3
  1309. (10 rows)
  1310. SELECT sum(unique1) over (order by four range between 2::int8 preceding and 1::int2 preceding exclude current row),
  1311. unique1, four
  1312. FROM tenk1 WHERE unique1 < 10;
  1313. sum | unique1 | four
  1314. -----+---------+------
  1315. | 0 | 0
  1316. | 8 | 0
  1317. | 4 | 0
  1318. 12 | 5 | 1
  1319. 12 | 9 | 1
  1320. 12 | 1 | 1
  1321. 27 | 6 | 2
  1322. 27 | 2 | 2
  1323. 23 | 3 | 3
  1324. 23 | 7 | 3
  1325. (10 rows)
  1326. SELECT sum(unique1) over (order by four range between 2::int8 preceding and 1::int2 preceding exclude group),
  1327. unique1, four
  1328. FROM tenk1 WHERE unique1 < 10;
  1329. sum | unique1 | four
  1330. -----+---------+------
  1331. | 0 | 0
  1332. | 8 | 0
  1333. | 4 | 0
  1334. 12 | 5 | 1
  1335. 12 | 9 | 1
  1336. 12 | 1 | 1
  1337. 27 | 6 | 2
  1338. 27 | 2 | 2
  1339. 23 | 3 | 3
  1340. 23 | 7 | 3
  1341. (10 rows)
  1342. SELECT sum(unique1) over (order by four range between 2::int8 preceding and 1::int2 preceding exclude ties),
  1343. unique1, four
  1344. FROM tenk1 WHERE unique1 < 10;
  1345. sum | unique1 | four
  1346. -----+---------+------
  1347. | 0 | 0
  1348. | 8 | 0
  1349. | 4 | 0
  1350. 12 | 5 | 1
  1351. 12 | 9 | 1
  1352. 12 | 1 | 1
  1353. 27 | 6 | 2
  1354. 27 | 2 | 2
  1355. 23 | 3 | 3
  1356. 23 | 7 | 3
  1357. (10 rows)
  1358. SELECT sum(unique1) over (order by four range between 2::int8 preceding and 6::int2 following exclude ties),
  1359. unique1, four
  1360. FROM tenk1 WHERE unique1 < 10;
  1361. sum | unique1 | four
  1362. -----+---------+------
  1363. 33 | 0 | 0
  1364. 41 | 8 | 0
  1365. 37 | 4 | 0
  1366. 35 | 5 | 1
  1367. 39 | 9 | 1
  1368. 31 | 1 | 1
  1369. 43 | 6 | 2
  1370. 39 | 2 | 2
  1371. 26 | 3 | 3
  1372. 30 | 7 | 3
  1373. (10 rows)
  1374. SELECT sum(unique1) over (order by four range between 2::int8 preceding and 6::int2 following exclude group),
  1375. unique1, four
  1376. FROM tenk1 WHERE unique1 < 10;
  1377. sum | unique1 | four
  1378. -----+---------+------
  1379. 33 | 0 | 0
  1380. 33 | 8 | 0
  1381. 33 | 4 | 0
  1382. 30 | 5 | 1
  1383. 30 | 9 | 1
  1384. 30 | 1 | 1
  1385. 37 | 6 | 2
  1386. 37 | 2 | 2
  1387. 23 | 3 | 3
  1388. 23 | 7 | 3
  1389. (10 rows)
  1390. SELECT sum(unique1) over (partition by four order by unique1 range between 5::int8 preceding and 6::int2 following),
  1391. unique1, four
  1392. FROM tenk1 WHERE unique1 < 10;
  1393. sum | unique1 | four
  1394. -----+---------+------
  1395. 4 | 0 | 0
  1396. 12 | 4 | 0
  1397. 12 | 8 | 0
  1398. 6 | 1 | 1
  1399. 15 | 5 | 1
  1400. 14 | 9 | 1
  1401. 8 | 2 | 2
  1402. 8 | 6 | 2
  1403. 10 | 3 | 3
  1404. 10 | 7 | 3
  1405. (10 rows)
  1406. SELECT sum(unique1) over (partition by four order by unique1 range between 5::int8 preceding and 6::int2 following
  1407. exclude current row),unique1, four
  1408. FROM tenk1 WHERE unique1 < 10;
  1409. sum | unique1 | four
  1410. -----+---------+------
  1411. 4 | 0 | 0
  1412. 8 | 4 | 0
  1413. 4 | 8 | 0
  1414. 5 | 1 | 1
  1415. 10 | 5 | 1
  1416. 5 | 9 | 1
  1417. 6 | 2 | 2
  1418. 2 | 6 | 2
  1419. 7 | 3 | 3
  1420. 3 | 7 | 3
  1421. (10 rows)
  1422. select sum(salary) over (order by enroll_date range between '1 year'::interval preceding and '1 year'::interval following),
  1423. salary, enroll_date from empsalary;
  1424. sum | salary | enroll_date
  1425. -------+--------+-------------
  1426. 34900 | 5000 | 10-01-2006
  1427. 34900 | 6000 | 10-01-2006
  1428. 38400 | 3900 | 12-23-2006
  1429. 47100 | 4800 | 08-01-2007
  1430. 47100 | 5200 | 08-01-2007
  1431. 47100 | 4800 | 08-08-2007
  1432. 47100 | 5200 | 08-15-2007
  1433. 36100 | 3500 | 12-10-2007
  1434. 32200 | 4500 | 01-01-2008
  1435. 32200 | 4200 | 01-01-2008
  1436. (10 rows)
  1437. select sum(salary) over (order by enroll_date desc range between '1 year'::interval preceding and '1 year'::interval following),
  1438. salary, enroll_date from empsalary;
  1439. sum | salary | enroll_date
  1440. -------+--------+-------------
  1441. 32200 | 4200 | 01-01-2008
  1442. 32200 | 4500 | 01-01-2008
  1443. 36100 | 3500 | 12-10-2007
  1444. 47100 | 5200 | 08-15-2007
  1445. 47100 | 4800 | 08-08-2007
  1446. 47100 | 4800 | 08-01-2007
  1447. 47100 | 5200 | 08-01-2007
  1448. 38400 | 3900 | 12-23-2006
  1449. 34900 | 5000 | 10-01-2006
  1450. 34900 | 6000 | 10-01-2006
  1451. (10 rows)
  1452. select sum(salary) over (order by enroll_date desc range between '1 year'::interval following and '1 year'::interval following),
  1453. salary, enroll_date from empsalary;
  1454. sum | salary | enroll_date
  1455. -----+--------+-------------
  1456. | 4200 | 01-01-2008
  1457. | 4500 | 01-01-2008
  1458. | 3500 | 12-10-2007
  1459. | 5200 | 08-15-2007
  1460. | 4800 | 08-08-2007
  1461. | 4800 | 08-01-2007
  1462. | 5200 | 08-01-2007
  1463. | 3900 | 12-23-2006
  1464. | 5000 | 10-01-2006
  1465. | 6000 | 10-01-2006
  1466. (10 rows)
  1467. select sum(salary) over (order by enroll_date range between '1 year'::interval preceding and '1 year'::interval following
  1468. exclude current row), salary, enroll_date from empsalary;
  1469. sum | salary | enroll_date
  1470. -------+--------+-------------
  1471. 29900 | 5000 | 10-01-2006
  1472. 28900 | 6000 | 10-01-2006
  1473. 34500 | 3900 | 12-23-2006
  1474. 42300 | 4800 | 08-01-2007
  1475. 41900 | 5200 | 08-01-2007
  1476. 42300 | 4800 | 08-08-2007
  1477. 41900 | 5200 | 08-15-2007
  1478. 32600 | 3500 | 12-10-2007
  1479. 27700 | 4500 | 01-01-2008
  1480. 28000 | 4200 | 01-01-2008
  1481. (10 rows)
  1482. select sum(salary) over (order by enroll_date range between '1 year'::interval preceding and '1 year'::interval following
  1483. exclude group), salary, enroll_date from empsalary;
  1484. sum | salary | enroll_date
  1485. -------+--------+-------------
  1486. 23900 | 5000 | 10-01-2006
  1487. 23900 | 6000 | 10-01-2006
  1488. 34500 | 3900 | 12-23-2006
  1489. 37100 | 4800 | 08-01-2007
  1490. 37100 | 5200 | 08-01-2007
  1491. 42300 | 4800 | 08-08-2007
  1492. 41900 | 5200 | 08-15-2007
  1493. 32600 | 3500 | 12-10-2007
  1494. 23500 | 4500 | 01-01-2008
  1495. 23500 | 4200 | 01-01-2008
  1496. (10 rows)
  1497. select sum(salary) over (order by enroll_date range between '1 year'::interval preceding and '1 year'::interval following
  1498. exclude ties), salary, enroll_date from empsalary;
  1499. sum | salary | enroll_date
  1500. -------+--------+-------------
  1501. 28900 | 5000 | 10-01-2006
  1502. 29900 | 6000 | 10-01-2006
  1503. 38400 | 3900 | 12-23-2006
  1504. 41900 | 4800 | 08-01-2007
  1505. 42300 | 5200 | 08-01-2007
  1506. 47100 | 4800 | 08-08-2007
  1507. 47100 | 5200 | 08-15-2007
  1508. 36100 | 3500 | 12-10-2007
  1509. 28000 | 4500 | 01-01-2008
  1510. 27700 | 4200 | 01-01-2008
  1511. (10 rows)
  1512. select first_value(salary) over(order by salary range between 1000 preceding and 1000 following),
  1513. lead(salary) over(order by salary range between 1000 preceding and 1000 following),
  1514. nth_value(salary, 1) over(order by salary range between 1000 preceding and 1000 following),
  1515. salary from empsalary;
  1516. first_value | lead | nth_value | salary
  1517. -------------+------+-----------+--------
  1518. 3500 | 3900 | 3500 | 3500
  1519. 3500 | 4200 | 3500 | 3900
  1520. 3500 | 4500 | 3500 | 4200
  1521. 3500 | 4800 | 3500 | 4500
  1522. 3900 | 4800 | 3900 | 4800
  1523. 3900 | 5000 | 3900 | 4800
  1524. 4200 | 5200 | 4200 | 5000
  1525. 4200 | 5200 | 4200 | 5200
  1526. 4200 | 6000 | 4200 | 5200
  1527. 5000 | | 5000 | 6000
  1528. (10 rows)
  1529. select last_value(salary) over(order by salary range between 1000 preceding and 1000 following),
  1530. lag(salary) over(order by salary range between 1000 preceding and 1000 following),
  1531. salary from empsalary;
  1532. last_value | lag | salary
  1533. ------------+------+--------
  1534. 4500 | | 3500
  1535. 4800 | 3500 | 3900
  1536. 5200 | 3900 | 4200
  1537. 5200 | 4200 | 4500
  1538. 5200 | 4500 | 4800
  1539. 5200 | 4800 | 4800
  1540. 6000 | 4800 | 5000
  1541. 6000 | 5000 | 5200
  1542. 6000 | 5200 | 5200
  1543. 6000 | 5200 | 6000
  1544. (10 rows)
  1545. select first_value(salary) over(order by salary range between 1000 following and 3000 following
  1546. exclude current row),
  1547. lead(salary) over(order by salary range between 1000 following and 3000 following exclude ties),
  1548. nth_value(salary, 1) over(order by salary range between 1000 following and 3000 following
  1549. exclude ties),
  1550. salary from empsalary;
  1551. first_value | lead | nth_value | salary
  1552. -------------+------+-----------+--------
  1553. 4500 | 3900 | 4500 | 3500
  1554. 5000 | 4200 | 5000 | 3900
  1555. 5200 | 4500 | 5200 | 4200
  1556. 6000 | 4800 | 6000 | 4500
  1557. 6000 | 4800 | 6000 | 4800
  1558. 6000 | 5000 | 6000 | 4800
  1559. 6000 | 5200 | 6000 | 5000
  1560. | 5200 | | 5200
  1561. | 6000 | | 5200
  1562. | | | 6000
  1563. (10 rows)
  1564. select last_value(salary) over(order by salary range between 1000 following and 3000 following
  1565. exclude group),
  1566. lag(salary) over(order by salary range between 1000 following and 3000 following exclude group),
  1567. salary from empsalary;
  1568. last_value | lag | salary
  1569. ------------+------+--------
  1570. 6000 | | 3500
  1571. 6000 | 3500 | 3900
  1572. 6000 | 3900 | 4200
  1573. 6000 | 4200 | 4500
  1574. 6000 | 4500 | 4800
  1575. 6000 | 4800 | 4800
  1576. 6000 | 4800 | 5000
  1577. | 5000 | 5200
  1578. | 5200 | 5200
  1579. | 5200 | 6000
  1580. (10 rows)
  1581. select first_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following
  1582. exclude ties),
  1583. last_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following),
  1584. salary, enroll_date from empsalary;
  1585. first_value | last_value | salary | enroll_date
  1586. -------------+------------+--------+-------------
  1587. 5000 | 5200 | 5000 | 10-01-2006
  1588. 6000 | 5200 | 6000 | 10-01-2006
  1589. 5000 | 3500 | 3900 | 12-23-2006
  1590. 5000 | 4200 | 4800 | 08-01-2007
  1591. 5000 | 4200 | 5200 | 08-01-2007
  1592. 5000 | 4200 | 4800 | 08-08-2007
  1593. 5000 | 4200 | 5200 | 08-15-2007
  1594. 5000 | 4200 | 3500 | 12-10-2007
  1595. 5000 | 4200 | 4500 | 01-01-2008
  1596. 5000 | 4200 | 4200 | 01-01-2008
  1597. (10 rows)
  1598. select first_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following
  1599. exclude ties),
  1600. last_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following
  1601. exclude ties),
  1602. salary, enroll_date from empsalary;
  1603. first_value | last_value | salary | enroll_date
  1604. -------------+------------+--------+-------------
  1605. 5000 | 5200 | 5000 | 10-01-2006
  1606. 6000 | 5200 | 6000 | 10-01-2006
  1607. 5000 | 3500 | 3900 | 12-23-2006
  1608. 5000 | 4200 | 4800 | 08-01-2007
  1609. 5000 | 4200 | 5200 | 08-01-2007
  1610. 5000 | 4200 | 4800 | 08-08-2007
  1611. 5000 | 4200 | 5200 | 08-15-2007
  1612. 5000 | 4200 | 3500 | 12-10-2007
  1613. 5000 | 4500 | 4500 | 01-01-2008
  1614. 5000 | 4200 | 4200 | 01-01-2008
  1615. (10 rows)
  1616. select first_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following
  1617. exclude group),
  1618. last_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following
  1619. exclude group),
  1620. salary, enroll_date from empsalary;
  1621. first_value | last_value | salary | enroll_date
  1622. -------------+------------+--------+-------------
  1623. 3900 | 5200 | 5000 | 10-01-2006
  1624. 3900 | 5200 | 6000 | 10-01-2006
  1625. 5000 | 3500 | 3900 | 12-23-2006
  1626. 5000 | 4200 | 4800 | 08-01-2007
  1627. 5000 | 4200 | 5200 | 08-01-2007
  1628. 5000 | 4200 | 4800 | 08-08-2007
  1629. 5000 | 4200 | 5200 | 08-15-2007
  1630. 5000 | 4200 | 3500 | 12-10-2007
  1631. 5000 | 3500 | 4500 | 01-01-2008
  1632. 5000 | 3500 | 4200 | 01-01-2008
  1633. (10 rows)
  1634. select first_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following
  1635. exclude current row),
  1636. last_value(salary) over(order by enroll_date range between unbounded preceding and '1 year'::interval following
  1637. exclude current row),
  1638. salary, enroll_date from empsalary;
  1639. first_value | last_value | salary | enroll_date
  1640. -------------+------------+--------+-------------
  1641. 6000 | 5200 | 5000 | 10-01-2006
  1642. 5000 | 5200 | 6000 | 10-01-2006
  1643. 5000 | 3500 | 3900 | 12-23-2006
  1644. 5000 | 4200 | 4800 | 08-01-2007
  1645. 5000 | 4200 | 5200 | 08-01-2007
  1646. 5000 | 4200 | 4800 | 08-08-2007
  1647. 5000 | 4200 | 5200 | 08-15-2007
  1648. 5000 | 4200 | 3500 | 12-10-2007
  1649. 5000 | 4200 | 4500 | 01-01-2008
  1650. 5000 | 4500 | 4200 | 01-01-2008
  1651. (10 rows)
  1652. -- RANGE offset PRECEDING/FOLLOWING with null values
  1653. select x, y,
  1654. first_value(y) over w,
  1655. last_value(y) over w
  1656. from
  1657. (select x, x as y from generate_series(1,5) as x
  1658. union all select null, 42
  1659. union all select null, 43) ss
  1660. window w as
  1661. (order by x asc nulls first range between 2 preceding and 2 following);
  1662. x | y | first_value | last_value
  1663. ---+----+-------------+------------
  1664. | 42 | 42 | 43
  1665. | 43 | 42 | 43
  1666. 1 | 1 | 1 | 3
  1667. 2 | 2 | 1 | 4
  1668. 3 | 3 | 1 | 5
  1669. 4 | 4 | 2 | 5
  1670. 5 | 5 | 3 | 5
  1671. (7 rows)
  1672. select x, y,
  1673. first_value(y) over w,
  1674. last_value(y) over w
  1675. from
  1676. (select x, x as y from generate_series(1,5) as x
  1677. union all select null, 42
  1678. union all select null, 43) ss
  1679. window w as
  1680. (order by x asc nulls last range between 2 preceding and 2 following);
  1681. x | y | first_value | last_value
  1682. ---+----+-------------+------------
  1683. 1 | 1 | 1 | 3
  1684. 2 | 2 | 1 | 4
  1685. 3 | 3 | 1 | 5
  1686. 4 | 4 | 2 | 5
  1687. 5 | 5 | 3 | 5
  1688. | 42 | 42 | 43
  1689. | 43 | 42 | 43
  1690. (7 rows)
  1691. select x, y,
  1692. first_value(y) over w,
  1693. last_value(y) over w
  1694. from
  1695. (select x, x as y from generate_series(1,5) as x
  1696. union all select null, 42
  1697. union all select null, 43) ss
  1698. window w as
  1699. (order by x desc nulls first range between 2 preceding and 2 following);
  1700. x | y | first_value | last_value
  1701. ---+----+-------------+------------
  1702. | 43 | 43 | 42
  1703. | 42 | 43 | 42
  1704. 5 | 5 | 5 | 3
  1705. 4 | 4 | 5 | 2
  1706. 3 | 3 | 5 | 1
  1707. 2 | 2 | 4 | 1
  1708. 1 | 1 | 3 | 1
  1709. (7 rows)
  1710. select x, y,
  1711. first_value(y) over w,
  1712. last_value(y) over w
  1713. from
  1714. (select x, x as y from generate_series(1,5) as x
  1715. union all select null, 42
  1716. union all select null, 43) ss
  1717. window w as
  1718. (order by x desc nulls last range between 2 preceding and 2 following);
  1719. x | y | first_value | last_value
  1720. ---+----+-------------+------------
  1721. 5 | 5 | 5 | 3
  1722. 4 | 4 | 5 | 2
  1723. 3 | 3 | 5 | 1
  1724. 2 | 2 | 4 | 1
  1725. 1 | 1 | 3 | 1
  1726. | 42 | 42 | 43
  1727. | 43 | 42 | 43
  1728. (7 rows)
  1729. -- Check overflow behavior for various integer sizes
  1730. select x, last_value(x) over (order by x::smallint range between current row and 2147450884 following)
  1731. from generate_series(32764, 32766) x;
  1732. x | last_value
  1733. -------+------------
  1734. 32764 | 32766
  1735. 32765 | 32766
  1736. 32766 | 32766
  1737. (3 rows)
  1738. select x, last_value(x) over (order by x::smallint desc range between current row and 2147450885 following)
  1739. from generate_series(-32766, -32764) x;
  1740. x | last_value
  1741. --------+------------
  1742. -32764 | -32766
  1743. -32765 | -32766
  1744. -32766 | -32766
  1745. (3 rows)
  1746. select x, last_value(x) over (order by x range between current row and 4 following)
  1747. from generate_series(2147483644, 2147483646) x;
  1748. x | last_value
  1749. ------------+------------
  1750. 2147483644 | 2147483646
  1751. 2147483645 | 2147483646
  1752. 2147483646 | 2147483646
  1753. (3 rows)
  1754. select x, last_value(x) over (order by x desc range between current row and 5 following)
  1755. from generate_series(-2147483646, -2147483644) x;
  1756. x | last_value
  1757. -------------+-------------
  1758. -2147483644 | -2147483646
  1759. -2147483645 | -2147483646
  1760. -2147483646 | -2147483646
  1761. (3 rows)
  1762. select x, last_value(x) over (order by x range between current row and 4 following)
  1763. from generate_series(9223372036854775804, 9223372036854775806) x;
  1764. x | last_value
  1765. ---------------------+---------------------
  1766. 9223372036854775804 | 9223372036854775806
  1767. 9223372036854775805 | 9223372036854775806
  1768. 9223372036854775806 | 9223372036854775806
  1769. (3 rows)
  1770. select x, last_value(x) over (order by x desc range between current row and 5 following)
  1771. from generate_series(-9223372036854775806, -9223372036854775804) x;
  1772. x | last_value
  1773. ----------------------+----------------------
  1774. -9223372036854775804 | -9223372036854775806
  1775. -9223372036854775805 | -9223372036854775806
  1776. -9223372036854775806 | -9223372036854775806
  1777. (3 rows)
  1778. -- Test in_range for other numeric datatypes
  1779. create temp table numerics(
  1780. id int,
  1781. f_float4 float4,
  1782. f_float8 float8,
  1783. f_numeric numeric
  1784. );
  1785. insert into numerics values
  1786. (0, '-infinity', '-infinity', '-infinity'),
  1787. (1, -3, -3, -3),
  1788. (2, -1, -1, -1),
  1789. (3, 0, 0, 0),
  1790. (4, 1.1, 1.1, 1.1),
  1791. (5, 1.12, 1.12, 1.12),
  1792. (6, 2, 2, 2),
  1793. (7, 100, 100, 100),
  1794. (8, 'infinity', 'infinity', 'infinity'),
  1795. (9, 'NaN', 'NaN', 'NaN');
  1796. select id, f_float4, first_value(id) over w, last_value(id) over w
  1797. from numerics
  1798. window w as (order by f_float4 range between
  1799. 1 preceding and 1 following);
  1800. id | f_float4 | first_value | last_value
  1801. ----+-----------+-------------+------------
  1802. 0 | -Infinity | 0 | 0
  1803. 1 | -3 | 1 | 1
  1804. 2 | -1 | 2 | 3
  1805. 3 | 0 | 2 | 3
  1806. 4 | 1.1 | 4 | 6
  1807. 5 | 1.12 | 4 | 6
  1808. 6 | 2 | 4 | 6
  1809. 7 | 100 | 7 | 7
  1810. 8 | Infinity | 8 | 8
  1811. 9 | NaN | 9 | 9
  1812. (10 rows)
  1813. select id, f_float4, first_value(id) over w, last_value(id) over w
  1814. from numerics
  1815. window w as (order by f_float4 range between
  1816. 1 preceding and 1.1::float4 following);
  1817. id | f_float4 | first_value | last_value
  1818. ----+-----------+-------------+------------
  1819. 0 | -Infinity | 0 | 0
  1820. 1 | -3 | 1 | 1
  1821. 2 | -1 | 2 | 3
  1822. 3 | 0 | 2 | 4
  1823. 4 | 1.1 | 4 | 6
  1824. 5 | 1.12 | 4 | 6
  1825. 6 | 2 | 4 | 6
  1826. 7 | 100 | 7 | 7
  1827. 8 | Infinity | 8 | 8
  1828. 9 | NaN | 9 | 9
  1829. (10 rows)
  1830. select id, f_float4, first_value(id) over w, last_value(id) over w
  1831. from numerics
  1832. window w as (order by f_float4 range between
  1833. 'inf' preceding and 'inf' following);
  1834. id | f_float4 | first_value | last_value
  1835. ----+-----------+-------------+------------
  1836. 0 | -Infinity | 0 | 8
  1837. 1 | -3 | 0 | 8
  1838. 2 | -1 | 0 | 8
  1839. 3 | 0 | 0 | 8
  1840. 4 | 1.1 | 0 | 8
  1841. 5 | 1.12 | 0 | 8
  1842. 6 | 2 | 0 | 8
  1843. 7 | 100 | 0 | 8
  1844. 8 | Infinity | 0 | 8
  1845. 9 | NaN | 9 | 9
  1846. (10 rows)
  1847. select id, f_float4, first_value(id) over w, last_value(id) over w
  1848. from numerics
  1849. window w as (order by f_float4 range between
  1850. 'inf' preceding and 'inf' preceding);
  1851. id | f_float4 | first_value | last_value
  1852. ----+-----------+-------------+------------
  1853. 0 | -Infinity | 0 | 0
  1854. 1 | -3 | 0 | 0
  1855. 2 | -1 | 0 | 0
  1856. 3 | 0 | 0 | 0
  1857. 4 | 1.1 | 0 | 0
  1858. 5 | 1.12 | 0 | 0
  1859. 6 | 2 | 0 | 0
  1860. 7 | 100 | 0 | 0
  1861. 8 | Infinity | 0 | 8
  1862. 9 | NaN | 9 | 9
  1863. (10 rows)
  1864. select id, f_float4, first_value(id) over w, last_value(id) over w
  1865. from numerics
  1866. window w as (order by f_float4 range between
  1867. 'inf' following and 'inf' following);
  1868. id | f_float4 | first_value | last_value
  1869. ----+-----------+-------------+------------
  1870. 0 | -Infinity | 0 | 8
  1871. 1 | -3 | 8 | 8
  1872. 2 | -1 | 8 | 8
  1873. 3 | 0 | 8 | 8
  1874. 4 | 1.1 | 8 | 8
  1875. 5 | 1.12 | 8 | 8
  1876. 6 | 2 | 8 | 8
  1877. 7 | 100 | 8 | 8
  1878. 8 | Infinity | 8 | 8
  1879. 9 | NaN | 9 | 9
  1880. (10 rows)
  1881. select id, f_float4, first_value(id) over w, last_value(id) over w
  1882. from numerics
  1883. window w as (order by f_float4 range between
  1884. 1.1 preceding and 'NaN' following); -- error, NaN disallowed
  1885. ERROR: invalid preceding or following size in window function
  1886. select id, f_float8, first_value(id) over w, last_value(id) over w
  1887. from numerics
  1888. window w as (order by f_float8 range between
  1889. 1 preceding and 1 following);
  1890. id | f_float8 | first_value | last_value
  1891. ----+-----------+-------------+------------
  1892. 0 | -Infinity | 0 | 0
  1893. 1 | -3 | 1 | 1
  1894. 2 | -1 | 2 | 3
  1895. 3 | 0 | 2 | 3
  1896. 4 | 1.1 | 4 | 6
  1897. 5 | 1.12 | 4 | 6
  1898. 6 | 2 | 4 | 6
  1899. 7 | 100 | 7 | 7
  1900. 8 | Infinity | 8 | 8
  1901. 9 | NaN | 9 | 9
  1902. (10 rows)
  1903. select id, f_float8, first_value(id) over w, last_value(id) over w
  1904. from numerics
  1905. window w as (order by f_float8 range between
  1906. 1 preceding and 1.1::float8 following);
  1907. id | f_float8 | first_value | last_value
  1908. ----+-----------+-------------+------------
  1909. 0 | -Infinity | 0 | 0
  1910. 1 | -3 | 1 | 1
  1911. 2 | -1 | 2 | 3
  1912. 3 | 0 | 2 | 4
  1913. 4 | 1.1 | 4 | 6
  1914. 5 | 1.12 | 4 | 6
  1915. 6 | 2 | 4 | 6
  1916. 7 | 100 | 7 | 7
  1917. 8 | Infinity | 8 | 8
  1918. 9 | NaN | 9 | 9
  1919. (10 rows)
  1920. select id, f_float8, first_value(id) over w, last_value(id) over w
  1921. from numerics
  1922. window w as (order by f_float8 range between
  1923. 'inf' preceding and 'inf' following);
  1924. id | f_float8 | first_value | last_value
  1925. ----+-----------+-------------+------------
  1926. 0 | -Infinity | 0 | 8
  1927. 1 | -3 | 0 | 8
  1928. 2 | -1 | 0 | 8
  1929. 3 | 0 | 0 | 8
  1930. 4 | 1.1 | 0 | 8
  1931. 5 | 1.12 | 0 | 8
  1932. 6 | 2 | 0 | 8
  1933. 7 | 100 | 0 | 8
  1934. 8 | Infinity | 0 | 8
  1935. 9 | NaN | 9 | 9
  1936. (10 rows)
  1937. select id, f_float8, first_value(id) over w, last_value(id) over w
  1938. from numerics
  1939. window w as (order by f_float8 range between
  1940. 'inf' preceding and 'inf' preceding);
  1941. id | f_float8 | first_value | last_value
  1942. ----+-----------+-------------+------------
  1943. 0 | -Infinity | 0 | 0
  1944. 1 | -3 | 0 | 0
  1945. 2 | -1 | 0 | 0
  1946. 3 | 0 | 0 | 0
  1947. 4 | 1.1 | 0 | 0
  1948. 5 | 1.12 | 0 | 0
  1949. 6 | 2 | 0 | 0
  1950. 7 | 100 | 0 | 0
  1951. 8 | Infinity | 0 | 8
  1952. 9 | NaN | 9 | 9
  1953. (10 rows)
  1954. select id, f_float8, first_value(id) over w, last_value(id) over w
  1955. from numerics
  1956. window w as (order by f_float8 range between
  1957. 'inf' following and 'inf' following);
  1958. id | f_float8 | first_value | last_value
  1959. ----+-----------+-------------+------------
  1960. 0 | -Infinity | 0 | 8
  1961. 1 | -3 | 8 | 8
  1962. 2 | -1 | 8 | 8
  1963. 3 | 0 | 8 | 8
  1964. 4 | 1.1 | 8 | 8
  1965. 5 | 1.12 | 8 | 8
  1966. 6 | 2 | 8 | 8
  1967. 7 | 100 | 8 | 8
  1968. 8 | Infinity | 8 | 8
  1969. 9 | NaN | 9 | 9
  1970. (10 rows)
  1971. select id, f_float8, first_value(id) over w, last_value(id) over w
  1972. from numerics
  1973. window w as (order by f_float8 range between
  1974. 1.1 preceding and 'NaN' following); -- error, NaN disallowed
  1975. ERROR: invalid preceding or following size in window function
  1976. select id, f_numeric, first_value(id) over w, last_value(id) over w
  1977. from numerics
  1978. window w as (order by f_numeric range between
  1979. 1 preceding and 1 following);
  1980. id | f_numeric | first_value | last_value
  1981. ----+-----------+-------------+------------
  1982. 0 | -Infinity | 0 | 0
  1983. 1 | -3 | 1 | 1
  1984. 2 | -1 | 2 | 3
  1985. 3 | 0 | 2 | 3
  1986. 4 | 1.1 | 4 | 6
  1987. 5 | 1.12 | 4 | 6
  1988. 6 | 2 | 4 | 6
  1989. 7 | 100 | 7 | 7
  1990. 8 | Infinity | 8 | 8
  1991. 9 | NaN | 9 | 9
  1992. (10 rows)
  1993. select id, f_numeric, first_value(id) over w, last_value(id) over w
  1994. from numerics
  1995. window w as (order by f_numeric range between
  1996. 1 preceding and 1.1::numeric following);
  1997. id | f_numeric | first_value | last_value
  1998. ----+-----------+-------------+------------
  1999. 0 | -Infinity | 0 | 0
  2000. 1 | -3 | 1 | 1
  2001. 2 | -1 | 2 | 3
  2002. 3 | 0 | 2 | 4
  2003. 4 | 1.1 | 4 | 6
  2004. 5 | 1.12 | 4 | 6
  2005. 6 | 2 | 4 | 6
  2006. 7 | 100 | 7 | 7
  2007. 8 | Infinity | 8 | 8
  2008. 9 | NaN | 9 | 9
  2009. (10 rows)
  2010. select id, f_numeric, first_value(id) over w, last_value(id) over w
  2011. from numerics
  2012. window w as (order by f_numeric range between
  2013. 1 preceding and 1.1::float8 following); -- currently unsupported
  2014. ERROR: RANGE with offset PRECEDING/FOLLOWING is not supported for column type numeric and offset type double precision
  2015. LINE 4: 1 preceding and 1.1::float8 following);
  2016. ^
  2017. HINT: Cast the offset value to an appropriate type.
  2018. select id, f_numeric, first_value(id) over w, last_value(id) over w
  2019. from numerics
  2020. window w as (order by f_numeric range between
  2021. 'inf' preceding and 'inf' following);
  2022. id | f_numeric | first_value | last_value
  2023. ----+-----------+-------------+------------
  2024. 0 | -Infinity | 0 | 8
  2025. 1 | -3 | 0 | 8
  2026. 2 | -1 | 0 | 8
  2027. 3 | 0 | 0 | 8
  2028. 4 | 1.1 | 0 | 8
  2029. 5 | 1.12 | 0 | 8
  2030. 6 | 2 | 0 | 8
  2031. 7 | 100 | 0 | 8
  2032. 8 | Infinity | 0 | 8
  2033. 9 | NaN | 9 | 9
  2034. (10 rows)
  2035. select id, f_numeric, first_value(id) over w, last_value(id) over w
  2036. from numerics
  2037. window w as (order by f_numeric range between
  2038. 'inf' preceding and 'inf' preceding);
  2039. id | f_numeric | first_value | last_value
  2040. ----+-----------+-------------+------------
  2041. 0 | -Infinity | 0 | 0
  2042. 1 | -3 | 0 | 0
  2043. 2 | -1 | 0 | 0
  2044. 3 | 0 | 0 | 0
  2045. 4 | 1.1 | 0 | 0
  2046. 5 | 1.12 | 0 | 0
  2047. 6 | 2 | 0 | 0
  2048. 7 | 100 | 0 | 0
  2049. 8 | Infinity | 0 | 8
  2050. 9 | NaN | 9 | 9
  2051. (10 rows)
  2052. select id, f_numeric, first_value(id) over w, last_value(id) over w
  2053. from numerics
  2054. window w as (order by f_numeric range between
  2055. 'inf' following and 'inf' following);
  2056. id | f_numeric | first_value | last_value
  2057. ----+-----------+-------------+------------
  2058. 0 | -Infinity | 0 | 8
  2059. 1 | -3 | 8 | 8
  2060. 2 | -1 | 8 | 8
  2061. 3 | 0 | 8 | 8
  2062. 4 | 1.1 | 8 | 8
  2063. 5 | 1.12 | 8 | 8
  2064. 6 | 2 | 8 | 8
  2065. 7 | 100 | 8 | 8
  2066. 8 | Infinity | 8 | 8
  2067. 9 | NaN | 9 | 9
  2068. (10 rows)
  2069. select id, f_numeric, first_value(id) over w, last_value(id) over w
  2070. from numerics
  2071. window w as (order by f_numeric range between
  2072. 1.1 preceding and 'NaN' following); -- error, NaN disallowed
  2073. ERROR: invalid preceding or following size in window function
  2074. -- Test in_range for other datetime datatypes
  2075. create temp table datetimes(
  2076. id int,
  2077. f_time time,
  2078. f_timetz timetz,
  2079. f_interval interval,
  2080. f_timestamptz timestamptz,
  2081. f_timestamp timestamp
  2082. );
  2083. insert into datetimes values
  2084. (1, '11:00', '11:00 BST', '1 year', '2000-10-19 10:23:54+01', '2000-10-19 10:23:54'),
  2085. (2, '12:00', '12:00 BST', '2 years', '2001-10-19 10:23:54+01', '2001-10-19 10:23:54'),
  2086. (3, '13:00', '13:00 BST', '3 years', '2001-10-19 10:23:54+01', '2001-10-19 10:23:54'),
  2087. (4, '14:00', '14:00 BST', '4 years', '2002-10-19 10:23:54+01', '2002-10-19 10:23:54'),
  2088. (5, '15:00', '15:00 BST', '5 years', '2003-10-19 10:23:54+01', '2003-10-19 10:23:54'),
  2089. (6, '15:00', '15:00 BST', '5 years', '2004-10-19 10:23:54+01', '2004-10-19 10:23:54'),
  2090. (7, '17:00', '17:00 BST', '7 years', '2005-10-19 10:23:54+01', '2005-10-19 10:23:54'),
  2091. (8, '18:00', '18:00 BST', '8 years', '2006-10-19 10:23:54+01', '2006-10-19 10:23:54'),
  2092. (9, '19:00', '19:00 BST', '9 years', '2007-10-19 10:23:54+01', '2007-10-19 10:23:54'),
  2093. (10, '20:00', '20:00 BST', '10 years', '2008-10-19 10:23:54+01', '2008-10-19 10:23:54');
  2094. select id, f_time, first_value(id) over w, last_value(id) over w
  2095. from datetimes
  2096. window w as (order by f_time range between
  2097. '70 min'::interval preceding and '2 hours'::interval following);
  2098. id | f_time | first_value | last_value
  2099. ----+----------+-------------+------------
  2100. 1 | 11:00:00 | 1 | 3
  2101. 2 | 12:00:00 | 1 | 4
  2102. 3 | 13:00:00 | 2 | 6
  2103. 4 | 14:00:00 | 3 | 6
  2104. 5 | 15:00:00 | 4 | 7
  2105. 6 | 15:00:00 | 4 | 7
  2106. 7 | 17:00:00 | 7 | 9
  2107. 8 | 18:00:00 | 7 | 10
  2108. 9 | 19:00:00 | 8 | 10
  2109. 10 | 20:00:00 | 9 | 10
  2110. (10 rows)
  2111. select id, f_time, first_value(id) over w, last_value(id) over w
  2112. from datetimes
  2113. window w as (order by f_time desc range between
  2114. '70 min' preceding and '2 hours' following);
  2115. id | f_time | first_value | last_value
  2116. ----+----------+-------------+------------
  2117. 10 | 20:00:00 | 10 | 8
  2118. 9 | 19:00:00 | 10 | 7
  2119. 8 | 18:00:00 | 9 | 7
  2120. 7 | 17:00:00 | 8 | 5
  2121. 6 | 15:00:00 | 6 | 3
  2122. 5 | 15:00:00 | 6 | 3
  2123. 4 | 14:00:00 | 6 | 2
  2124. 3 | 13:00:00 | 4 | 1
  2125. 2 | 12:00:00 | 3 | 1
  2126. 1 | 11:00:00 | 2 | 1
  2127. (10 rows)
  2128. select id, f_timetz, first_value(id) over w, last_value(id) over w
  2129. from datetimes
  2130. window w as (order by f_timetz range between
  2131. '70 min'::interval preceding and '2 hours'::interval following);
  2132. id | f_timetz | first_value | last_value
  2133. ----+-------------+-------------+------------
  2134. 1 | 11:00:00+01 | 1 | 3
  2135. 2 | 12:00:00+01 | 1 | 4
  2136. 3 | 13:00:00+01 | 2 | 6
  2137. 4 | 14:00:00+01 | 3 | 6
  2138. 5 | 15:00:00+01 | 4 | 7
  2139. 6 | 15:00:00+01 | 4 | 7
  2140. 7 | 17:00:00+01 | 7 | 9
  2141. 8 | 18:00:00+01 | 7 | 10
  2142. 9 | 19:00:00+01 | 8 | 10
  2143. 10 | 20:00:00+01 | 9 | 10
  2144. (10 rows)
  2145. select id, f_timetz, first_value(id) over w, last_value(id) over w
  2146. from datetimes
  2147. window w as (order by f_timetz desc range between
  2148. '70 min' preceding and '2 hours' following);
  2149. id | f_timetz | first_value | last_value
  2150. ----+-------------+-------------+------------
  2151. 10 | 20:00:00+01 | 10 | 8
  2152. 9 | 19:00:00+01 | 10 | 7
  2153. 8 | 18:00:00+01 | 9 | 7
  2154. 7 | 17:00:00+01 | 8 | 5
  2155. 6 | 15:00:00+01 | 6 | 3
  2156. 5 | 15:00:00+01 | 6 | 3
  2157. 4 | 14:00:00+01 | 6 | 2
  2158. 3 | 13:00:00+01 | 4 | 1
  2159. 2 | 12:00:00+01 | 3 | 1
  2160. 1 | 11:00:00+01 | 2 | 1
  2161. (10 rows)
  2162. select id, f_interval, first_value(id) over w, last_value(id) over w
  2163. from datetimes
  2164. window w as (order by f_interval range between
  2165. '1 year'::interval preceding and '1 year'::interval following);
  2166. id | f_interval | first_value | last_value
  2167. ----+------------+-------------+------------
  2168. 1 | @ 1 year | 1 | 2
  2169. 2 | @ 2 years | 1 | 3
  2170. 3 | @ 3 years | 2 | 4
  2171. 4 | @ 4 years | 3 | 6
  2172. 5 | @ 5 years | 4 | 6
  2173. 6 | @ 5 years | 4 | 6
  2174. 7 | @ 7 years | 7 | 8
  2175. 8 | @ 8 years | 7 | 9
  2176. 9 | @ 9 years | 8 | 10
  2177. 10 | @ 10 years | 9 | 10
  2178. (10 rows)
  2179. select id, f_interval, first_value(id) over w, last_value(id) over w
  2180. from datetimes
  2181. window w as (order by f_interval desc range between
  2182. '1 year' preceding and '1 year' following);
  2183. id | f_interval | first_value | last_value
  2184. ----+------------+-------------+------------
  2185. 10 | @ 10 years | 10 | 9
  2186. 9 | @ 9 years | 10 | 8
  2187. 8 | @ 8 years | 9 | 7
  2188. 7 | @ 7 years | 8 | 7
  2189. 6 | @ 5 years | 6 | 4
  2190. 5 | @ 5 years | 6 | 4
  2191. 4 | @ 4 years | 6 | 3
  2192. 3 | @ 3 years | 4 | 2
  2193. 2 | @ 2 years | 3 | 1
  2194. 1 | @ 1 year | 2 | 1
  2195. (10 rows)
  2196. select id, f_timestamptz, first_value(id) over w, last_value(id) over w
  2197. from datetimes
  2198. window w as (order by f_timestamptz range between
  2199. '1 year'::interval preceding and '1 year'::interval following);
  2200. id | f_timestamptz | first_value | last_value
  2201. ----+------------------------------+-------------+------------
  2202. 1 | Thu Oct 19 02:23:54 2000 PDT | 1 | 3
  2203. 2 | Fri Oct 19 02:23:54 2001 PDT | 1 | 4
  2204. 3 | Fri Oct 19 02:23:54 2001 PDT | 1 | 4
  2205. 4 | Sat Oct 19 02:23:54 2002 PDT | 2 | 5
  2206. 5 | Sun Oct 19 02:23:54 2003 PDT | 4 | 6
  2207. 6 | Tue Oct 19 02:23:54 2004 PDT | 5 | 7
  2208. 7 | Wed Oct 19 02:23:54 2005 PDT | 6 | 8
  2209. 8 | Thu Oct 19 02:23:54 2006 PDT | 7 | 9
  2210. 9 | Fri Oct 19 02:23:54 2007 PDT | 8 | 10
  2211. 10 | Sun Oct 19 02:23:54 2008 PDT | 9 | 10
  2212. (10 rows)
  2213. select id, f_timestamptz, first_value(id) over w, last_value(id) over w
  2214. from datetimes
  2215. window w as (order by f_timestamptz desc range between
  2216. '1 year' preceding and '1 year' following);
  2217. id | f_timestamptz | first_value | last_value
  2218. ----+------------------------------+-------------+------------
  2219. 10 | Sun Oct 19 02:23:54 2008 PDT | 10 | 9
  2220. 9 | Fri Oct 19 02:23:54 2007 PDT | 10 | 8
  2221. 8 | Thu Oct 19 02:23:54 2006 PDT | 9 | 7
  2222. 7 | Wed Oct 19 02:23:54 2005 PDT | 8 | 6
  2223. 6 | Tue Oct 19 02:23:54 2004 PDT | 7 | 5
  2224. 5 | Sun Oct 19 02:23:54 2003 PDT | 6 | 4
  2225. 4 | Sat Oct 19 02:23:54 2002 PDT | 5 | 2
  2226. 3 | Fri Oct 19 02:23:54 2001 PDT | 4 | 1
  2227. 2 | Fri Oct 19 02:23:54 2001 PDT | 4 | 1
  2228. 1 | Thu Oct 19 02:23:54 2000 PDT | 3 | 1
  2229. (10 rows)
  2230. select id, f_timestamp, first_value(id) over w, last_value(id) over w
  2231. from datetimes
  2232. window w as (order by f_timestamp range between
  2233. '1 year'::interval preceding and '1 year'::interval following);
  2234. id | f_timestamp | first_value | last_value
  2235. ----+--------------------------+-------------+------------
  2236. 1 | Thu Oct 19 10:23:54 2000 | 1 | 3
  2237. 2 | Fri Oct 19 10:23:54 2001 | 1 | 4
  2238. 3 | Fri Oct 19 10:23:54 2001 | 1 | 4
  2239. 4 | Sat Oct 19 10:23:54 2002 | 2 | 5
  2240. 5 | Sun Oct 19 10:23:54 2003 | 4 | 6
  2241. 6 | Tue Oct 19 10:23:54 2004 | 5 | 7
  2242. 7 | Wed Oct 19 10:23:54 2005 | 6 | 8
  2243. 8 | Thu Oct 19 10:23:54 2006 | 7 | 9
  2244. 9 | Fri Oct 19 10:23:54 2007 | 8 | 10
  2245. 10 | Sun Oct 19 10:23:54 2008 | 9 | 10
  2246. (10 rows)
  2247. select id, f_timestamp, first_value(id) over w, last_value(id) over w
  2248. from datetimes
  2249. window w as (order by f_timestamp desc range between
  2250. '1 year' preceding and '1 year' following);
  2251. id | f_timestamp | first_value | last_value
  2252. ----+--------------------------+-------------+------------
  2253. 10 | Sun Oct 19 10:23:54 2008 | 10 | 9
  2254. 9 | Fri Oct 19 10:23:54 2007 | 10 | 8
  2255. 8 | Thu Oct 19 10:23:54 2006 | 9 | 7
  2256. 7 | Wed Oct 19 10:23:54 2005 | 8 | 6
  2257. 6 | Tue Oct 19 10:23:54 2004 | 7 | 5
  2258. 5 | Sun Oct 19 10:23:54 2003 | 6 | 4
  2259. 4 | Sat Oct 19 10:23:54 2002 | 5 | 2
  2260. 3 | Fri Oct 19 10:23:54 2001 | 4 | 1
  2261. 2 | Fri Oct 19 10:23:54 2001 | 4 | 1
  2262. 1 | Thu Oct 19 10:23:54 2000 | 3 | 1
  2263. (10 rows)
  2264. -- RANGE offset PRECEDING/FOLLOWING error cases
  2265. select sum(salary) over (order by enroll_date, salary range between '1 year'::interval preceding and '2 years'::interval following
  2266. exclude ties), salary, enroll_date from empsalary;
  2267. ERROR: RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column
  2268. LINE 1: select sum(salary) over (order by enroll_date, salary range ...
  2269. ^
  2270. select sum(salary) over (range between '1 year'::interval preceding and '2 years'::interval following
  2271. exclude ties), salary, enroll_date from empsalary;
  2272. ERROR: RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column
  2273. LINE 1: select sum(salary) over (range between '1 year'::interval pr...
  2274. ^
  2275. select sum(salary) over (order by depname range between '1 year'::interval preceding and '2 years'::interval following
  2276. exclude ties), salary, enroll_date from empsalary;
  2277. ERROR: RANGE with offset PRECEDING/FOLLOWING is not supported for column type text
  2278. LINE 1: ... sum(salary) over (order by depname range between '1 year'::...
  2279. ^
  2280. select max(enroll_date) over (order by enroll_date range between 1 preceding and 2 following
  2281. exclude ties), salary, enroll_date from empsalary;
  2282. ERROR: RANGE with offset PRECEDING/FOLLOWING is not supported for column type date and offset type integer
  2283. LINE 1: ...ll_date) over (order by enroll_date range between 1 precedin...
  2284. ^
  2285. HINT: Cast the offset value to an appropriate type.
  2286. select max(enroll_date) over (order by salary range between -1 preceding and 2 following
  2287. exclude ties), salary, enroll_date from empsalary;
  2288. ERROR: invalid preceding or following size in window function
  2289. select max(enroll_date) over (order by salary range between 1 preceding and -2 following
  2290. exclude ties), salary, enroll_date from empsalary;
  2291. ERROR: invalid preceding or following size in window function
  2292. select max(enroll_date) over (order by salary range between '1 year'::interval preceding and '2 years'::interval following
  2293. exclude ties), salary, enroll_date from empsalary;
  2294. ERROR: RANGE with offset PRECEDING/FOLLOWING is not supported for column type integer and offset type interval
  2295. LINE 1: ...(enroll_date) over (order by salary range between '1 year'::...
  2296. ^
  2297. HINT: Cast the offset value to an appropriate type.
  2298. select max(enroll_date) over (order by enroll_date range between '1 year'::interval preceding and '-2 years'::interval following
  2299. exclude ties), salary, enroll_date from empsalary;
  2300. ERROR: invalid preceding or following size in window function
  2301. -- GROUPS tests
  2302. SELECT sum(unique1) over (order by four groups between unbounded preceding and current row),
  2303. unique1, four
  2304. FROM tenk1 WHERE unique1 < 10;
  2305. sum | unique1 | four
  2306. -----+---------+------
  2307. 12 | 0 | 0
  2308. 12 | 8 | 0
  2309. 12 | 4 | 0
  2310. 27 | 5 | 1
  2311. 27 | 9 | 1
  2312. 27 | 1 | 1
  2313. 35 | 6 | 2
  2314. 35 | 2 | 2
  2315. 45 | 3 | 3
  2316. 45 | 7 | 3
  2317. (10 rows)
  2318. SELECT sum(unique1) over (order by four groups between unbounded preceding and unbounded following),
  2319. unique1, four
  2320. FROM tenk1 WHERE unique1 < 10;
  2321. sum | unique1 | four
  2322. -----+---------+------
  2323. 45 | 0 | 0
  2324. 45 | 8 | 0
  2325. 45 | 4 | 0
  2326. 45 | 5 | 1
  2327. 45 | 9 | 1
  2328. 45 | 1 | 1
  2329. 45 | 6 | 2
  2330. 45 | 2 | 2
  2331. 45 | 3 | 3
  2332. 45 | 7 | 3
  2333. (10 rows)
  2334. SELECT sum(unique1) over (order by four groups between current row and unbounded following),
  2335. unique1, four
  2336. FROM tenk1 WHERE unique1 < 10;
  2337. sum | unique1 | four
  2338. -----+---------+------
  2339. 45 | 0 | 0
  2340. 45 | 8 | 0
  2341. 45 | 4 | 0
  2342. 33 | 5 | 1
  2343. 33 | 9 | 1
  2344. 33 | 1 | 1
  2345. 18 | 6 | 2
  2346. 18 | 2 | 2
  2347. 10 | 3 | 3
  2348. 10 | 7 | 3
  2349. (10 rows)
  2350. SELECT sum(unique1) over (order by four groups between 1 preceding and unbounded following),
  2351. unique1, four
  2352. FROM tenk1 WHERE unique1 < 10;
  2353. sum | unique1 | four
  2354. -----+---------+------
  2355. 45 | 0 | 0
  2356. 45 | 8 | 0
  2357. 45 | 4 | 0
  2358. 45 | 5 | 1
  2359. 45 | 9 | 1
  2360. 45 | 1 | 1
  2361. 33 | 6 | 2
  2362. 33 | 2 | 2
  2363. 18 | 3 | 3
  2364. 18 | 7 | 3
  2365. (10 rows)
  2366. SELECT sum(unique1) over (order by four groups between 1 following and unbounded following),
  2367. unique1, four
  2368. FROM tenk1 WHERE unique1 < 10;
  2369. sum | unique1 | four
  2370. -----+---------+------
  2371. 33 | 0 | 0
  2372. 33 | 8 | 0
  2373. 33 | 4 | 0
  2374. 18 | 5 | 1
  2375. 18 | 9 | 1
  2376. 18 | 1 | 1
  2377. 10 | 6 | 2
  2378. 10 | 2 | 2
  2379. | 3 | 3
  2380. | 7 | 3
  2381. (10 rows)
  2382. SELECT sum(unique1) over (order by four groups between unbounded preceding and 2 following),
  2383. unique1, four
  2384. FROM tenk1 WHERE unique1 < 10;
  2385. sum | unique1 | four
  2386. -----+---------+------
  2387. 35 | 0 | 0
  2388. 35 | 8 | 0
  2389. 35 | 4 | 0
  2390. 45 | 5 | 1
  2391. 45 | 9 | 1
  2392. 45 | 1 | 1
  2393. 45 | 6 | 2
  2394. 45 | 2 | 2
  2395. 45 | 3 | 3
  2396. 45 | 7 | 3
  2397. (10 rows)
  2398. SELECT sum(unique1) over (order by four groups between 2 preceding and 1 preceding),
  2399. unique1, four
  2400. FROM tenk1 WHERE unique1 < 10;
  2401. sum | unique1 | four
  2402. -----+---------+------
  2403. | 0 | 0
  2404. | 8 | 0
  2405. | 4 | 0
  2406. 12 | 5 | 1
  2407. 12 | 9 | 1
  2408. 12 | 1 | 1
  2409. 27 | 6 | 2
  2410. 27 | 2 | 2
  2411. 23 | 3 | 3
  2412. 23 | 7 | 3
  2413. (10 rows)
  2414. SELECT sum(unique1) over (order by four groups between 2 preceding and 1 following),
  2415. unique1, four
  2416. FROM tenk1 WHERE unique1 < 10;
  2417. sum | unique1 | four
  2418. -----+---------+------
  2419. 27 | 0 | 0
  2420. 27 | 8 | 0
  2421. 27 | 4 | 0
  2422. 35 | 5 | 1
  2423. 35 | 9 | 1
  2424. 35 | 1 | 1
  2425. 45 | 6 | 2
  2426. 45 | 2 | 2
  2427. 33 | 3 | 3
  2428. 33 | 7 | 3
  2429. (10 rows)
  2430. SELECT sum(unique1) over (order by four groups between 0 preceding and 0 following),
  2431. unique1, four
  2432. FROM tenk1 WHERE unique1 < 10;
  2433. sum | unique1 | four
  2434. -----+---------+------
  2435. 12 | 0 | 0
  2436. 12 | 8 | 0
  2437. 12 | 4 | 0
  2438. 15 | 5 | 1
  2439. 15 | 9 | 1
  2440. 15 | 1 | 1
  2441. 8 | 6 | 2
  2442. 8 | 2 | 2
  2443. 10 | 3 | 3
  2444. 10 | 7 | 3
  2445. (10 rows)
  2446. SELECT sum(unique1) over (order by four groups between 2 preceding and 1 following
  2447. exclude current row), unique1, four
  2448. FROM tenk1 WHERE unique1 < 10;
  2449. sum | unique1 | four
  2450. -----+---------+------
  2451. 27 | 0 | 0
  2452. 19 | 8 | 0
  2453. 23 | 4 | 0
  2454. 30 | 5 | 1
  2455. 26 | 9 | 1
  2456. 34 | 1 | 1
  2457. 39 | 6 | 2
  2458. 43 | 2 | 2
  2459. 30 | 3 | 3
  2460. 26 | 7 | 3
  2461. (10 rows)
  2462. SELECT sum(unique1) over (order by four groups between 2 preceding and 1 following
  2463. exclude group), unique1, four
  2464. FROM tenk1 WHERE unique1 < 10;
  2465. sum | unique1 | four
  2466. -----+---------+------
  2467. 15 | 0 | 0
  2468. 15 | 8 | 0
  2469. 15 | 4 | 0
  2470. 20 | 5 | 1
  2471. 20 | 9 | 1
  2472. 20 | 1 | 1
  2473. 37 | 6 | 2
  2474. 37 | 2 | 2
  2475. 23 | 3 | 3
  2476. 23 | 7 | 3
  2477. (10 rows)
  2478. SELECT sum(unique1) over (order by four groups between 2 preceding and 1 following
  2479. exclude ties), unique1, four
  2480. FROM tenk1 WHERE unique1 < 10;
  2481. sum | unique1 | four
  2482. -----+---------+------
  2483. 15 | 0 | 0
  2484. 23 | 8 | 0
  2485. 19 | 4 | 0
  2486. 25 | 5 | 1
  2487. 29 | 9 | 1
  2488. 21 | 1 | 1
  2489. 43 | 6 | 2
  2490. 39 | 2 | 2
  2491. 26 | 3 | 3
  2492. 30 | 7 | 3
  2493. (10 rows)
  2494. SELECT sum(unique1) over (partition by ten
  2495. order by four groups between 0 preceding and 0 following),unique1, four, ten
  2496. FROM tenk1 WHERE unique1 < 10;
  2497. sum | unique1 | four | ten
  2498. -----+---------+------+-----
  2499. 0 | 0 | 0 | 0
  2500. 1 | 1 | 1 | 1
  2501. 2 | 2 | 2 | 2
  2502. 3 | 3 | 3 | 3
  2503. 4 | 4 | 0 | 4
  2504. 5 | 5 | 1 | 5
  2505. 6 | 6 | 2 | 6
  2506. 7 | 7 | 3 | 7
  2507. 8 | 8 | 0 | 8
  2508. 9 | 9 | 1 | 9
  2509. (10 rows)
  2510. SELECT sum(unique1) over (partition by ten
  2511. order by four groups between 0 preceding and 0 following exclude current row), unique1, four, ten
  2512. FROM tenk1 WHERE unique1 < 10;
  2513. sum | unique1 | four | ten
  2514. -----+---------+------+-----
  2515. | 0 | 0 | 0
  2516. | 1 | 1 | 1
  2517. | 2 | 2 | 2
  2518. | 3 | 3 | 3
  2519. | 4 | 0 | 4
  2520. | 5 | 1 | 5
  2521. | 6 | 2 | 6
  2522. | 7 | 3 | 7
  2523. | 8 | 0 | 8
  2524. | 9 | 1 | 9
  2525. (10 rows)
  2526. SELECT sum(unique1) over (partition by ten
  2527. order by four groups between 0 preceding and 0 following exclude group), unique1, four, ten
  2528. FROM tenk1 WHERE unique1 < 10;
  2529. sum | unique1 | four | ten
  2530. -----+---------+------+-----
  2531. | 0 | 0 | 0
  2532. | 1 | 1 | 1
  2533. | 2 | 2 | 2
  2534. | 3 | 3 | 3
  2535. | 4 | 0 | 4
  2536. | 5 | 1 | 5
  2537. | 6 | 2 | 6
  2538. | 7 | 3 | 7
  2539. | 8 | 0 | 8
  2540. | 9 | 1 | 9
  2541. (10 rows)
  2542. SELECT sum(unique1) over (partition by ten
  2543. order by four groups between 0 preceding and 0 following exclude ties), unique1, four, ten
  2544. FROM tenk1 WHERE unique1 < 10;
  2545. sum | unique1 | four | ten
  2546. -----+---------+------+-----
  2547. 0 | 0 | 0 | 0
  2548. 1 | 1 | 1 | 1
  2549. 2 | 2 | 2 | 2
  2550. 3 | 3 | 3 | 3
  2551. 4 | 4 | 0 | 4
  2552. 5 | 5 | 1 | 5
  2553. 6 | 6 | 2 | 6
  2554. 7 | 7 | 3 | 7
  2555. 8 | 8 | 0 | 8
  2556. 9 | 9 | 1 | 9
  2557. (10 rows)
  2558. select first_value(salary) over(order by enroll_date groups between 1 preceding and 1 following),
  2559. lead(salary) over(order by enroll_date groups between 1 preceding and 1 following),
  2560. nth_value(salary, 1) over(order by enroll_date groups between 1 preceding and 1 following),
  2561. salary, enroll_date from empsalary;
  2562. first_value | lead | nth_value | salary | enroll_date
  2563. -------------+------+-----------+--------+-------------
  2564. 5000 | 6000 | 5000 | 5000 | 10-01-2006
  2565. 5000 | 3900 | 5000 | 6000 | 10-01-2006
  2566. 5000 | 4800 | 5000 | 3900 | 12-23-2006
  2567. 3900 | 5200 | 3900 | 4800 | 08-01-2007
  2568. 3900 | 4800 | 3900 | 5200 | 08-01-2007
  2569. 4800 | 5200 | 4800 | 4800 | 08-08-2007
  2570. 4800 | 3500 | 4800 | 5200 | 08-15-2007
  2571. 5200 | 4500 | 5200 | 3500 | 12-10-2007
  2572. 3500 | 4200 | 3500 | 4500 | 01-01-2008
  2573. 3500 | | 3500 | 4200 | 01-01-2008
  2574. (10 rows)
  2575. select last_value(salary) over(order by enroll_date groups between 1 preceding and 1 following),
  2576. lag(salary) over(order by enroll_date groups between 1 preceding and 1 following),
  2577. salary, enroll_date from empsalary;
  2578. last_value | lag | salary | enroll_date
  2579. ------------+------+--------+-------------
  2580. 3900 | | 5000 | 10-01-2006
  2581. 3900 | 5000 | 6000 | 10-01-2006
  2582. 5200 | 6000 | 3900 | 12-23-2006
  2583. 4800 | 3900 | 4800 | 08-01-2007
  2584. 4800 | 4800 | 5200 | 08-01-2007
  2585. 5200 | 5200 | 4800 | 08-08-2007
  2586. 3500 | 4800 | 5200 | 08-15-2007
  2587. 4200 | 5200 | 3500 | 12-10-2007
  2588. 4200 | 3500 | 4500 | 01-01-2008
  2589. 4200 | 4500 | 4200 | 01-01-2008
  2590. (10 rows)
  2591. select first_value(salary) over(order by enroll_date groups between 1 following and 3 following
  2592. exclude current row),
  2593. lead(salary) over(order by enroll_date groups between 1 following and 3 following exclude ties),
  2594. nth_value(salary, 1) over(order by enroll_date groups between 1 following and 3 following
  2595. exclude ties),
  2596. salary, enroll_date from empsalary;
  2597. first_value | lead | nth_value | salary | enroll_date
  2598. -------------+------+-----------+--------+-------------
  2599. 3900 | 6000 | 3900 | 5000 | 10-01-2006
  2600. 3900 | 3900 | 3900 | 6000 | 10-01-2006
  2601. 4800 | 4800 | 4800 | 3900 | 12-23-2006
  2602. 4800 | 5200 | 4800 | 4800 | 08-01-2007
  2603. 4800 | 4800 | 4800 | 5200 | 08-01-2007
  2604. 5200 | 5200 | 5200 | 4800 | 08-08-2007
  2605. 3500 | 3500 | 3500 | 5200 | 08-15-2007
  2606. 4500 | 4500 | 4500 | 3500 | 12-10-2007
  2607. | 4200 | | 4500 | 01-01-2008
  2608. | | | 4200 | 01-01-2008
  2609. (10 rows)
  2610. select last_value(salary) over(order by enroll_date groups between 1 following and 3 following
  2611. exclude group),
  2612. lag(salary) over(order by enroll_date groups between 1 following and 3 following exclude group),
  2613. salary, enroll_date from empsalary;
  2614. last_value | lag | salary | enroll_date
  2615. ------------+------+--------+-------------
  2616. 4800 | | 5000 | 10-01-2006
  2617. 4800 | 5000 | 6000 | 10-01-2006
  2618. 5200 | 6000 | 3900 | 12-23-2006
  2619. 3500 | 3900 | 4800 | 08-01-2007
  2620. 3500 | 4800 | 5200 | 08-01-2007
  2621. 4200 | 5200 | 4800 | 08-08-2007
  2622. 4200 | 4800 | 5200 | 08-15-2007
  2623. 4200 | 5200 | 3500 | 12-10-2007
  2624. | 3500 | 4500 | 01-01-2008
  2625. | 4500 | 4200 | 01-01-2008
  2626. (10 rows)
  2627. -- Show differences in offset interpretation between ROWS, RANGE, and GROUPS
  2628. WITH cte (x) AS (
  2629. SELECT * FROM generate_series(1, 35, 2)
  2630. )
  2631. SELECT x, (sum(x) over w)
  2632. FROM cte
  2633. WINDOW w AS (ORDER BY x rows between 1 preceding and 1 following);
  2634. x | sum
  2635. ----+-----
  2636. 1 | 4
  2637. 3 | 9
  2638. 5 | 15
  2639. 7 | 21
  2640. 9 | 27
  2641. 11 | 33
  2642. 13 | 39
  2643. 15 | 45
  2644. 17 | 51
  2645. 19 | 57
  2646. 21 | 63
  2647. 23 | 69
  2648. 25 | 75
  2649. 27 | 81
  2650. 29 | 87
  2651. 31 | 93
  2652. 33 | 99
  2653. 35 | 68
  2654. (18 rows)
  2655. WITH cte (x) AS (
  2656. SELECT * FROM generate_series(1, 35, 2)
  2657. )
  2658. SELECT x, (sum(x) over w)
  2659. FROM cte
  2660. WINDOW w AS (ORDER BY x range between 1 preceding and 1 following);
  2661. x | sum
  2662. ----+-----
  2663. 1 | 1
  2664. 3 | 3
  2665. 5 | 5
  2666. 7 | 7
  2667. 9 | 9
  2668. 11 | 11
  2669. 13 | 13
  2670. 15 | 15
  2671. 17 | 17
  2672. 19 | 19
  2673. 21 | 21
  2674. 23 | 23
  2675. 25 | 25
  2676. 27 | 27
  2677. 29 | 29
  2678. 31 | 31
  2679. 33 | 33
  2680. 35 | 35
  2681. (18 rows)
  2682. WITH cte (x) AS (
  2683. SELECT * FROM generate_series(1, 35, 2)
  2684. )
  2685. SELECT x, (sum(x) over w)
  2686. FROM cte
  2687. WINDOW w AS (ORDER BY x groups between 1 preceding and 1 following);
  2688. x | sum
  2689. ----+-----
  2690. 1 | 4
  2691. 3 | 9
  2692. 5 | 15
  2693. 7 | 21
  2694. 9 | 27
  2695. 11 | 33
  2696. 13 | 39
  2697. 15 | 45
  2698. 17 | 51
  2699. 19 | 57
  2700. 21 | 63
  2701. 23 | 69
  2702. 25 | 75
  2703. 27 | 81
  2704. 29 | 87
  2705. 31 | 93
  2706. 33 | 99
  2707. 35 | 68
  2708. (18 rows)
  2709. WITH cte (x) AS (
  2710. select 1 union all select 1 union all select 1 union all
  2711. SELECT * FROM generate_series(5, 49, 2)
  2712. )
  2713. SELECT x, (sum(x) over w)
  2714. FROM cte
  2715. WINDOW w AS (ORDER BY x rows between 1 preceding and 1 following);
  2716. x | sum
  2717. ----+-----
  2718. 1 | 2
  2719. 1 | 3
  2720. 1 | 7
  2721. 5 | 13
  2722. 7 | 21
  2723. 9 | 27
  2724. 11 | 33
  2725. 13 | 39
  2726. 15 | 45
  2727. 17 | 51
  2728. 19 | 57
  2729. 21 | 63
  2730. 23 | 69
  2731. 25 | 75
  2732. 27 | 81
  2733. 29 | 87
  2734. 31 | 93
  2735. 33 | 99
  2736. 35 | 105
  2737. 37 | 111
  2738. 39 | 117
  2739. 41 | 123
  2740. 43 | 129
  2741. 45 | 135
  2742. 47 | 141
  2743. 49 | 96
  2744. (26 rows)
  2745. WITH cte (x) AS (
  2746. select 1 union all select 1 union all select 1 union all
  2747. SELECT * FROM generate_series(5, 49, 2)
  2748. )
  2749. SELECT x, (sum(x) over w)
  2750. FROM cte
  2751. WINDOW w AS (ORDER BY x range between 1 preceding and 1 following);
  2752. x | sum
  2753. ----+-----
  2754. 1 | 3
  2755. 1 | 3
  2756. 1 | 3
  2757. 5 | 5
  2758. 7 | 7
  2759. 9 | 9
  2760. 11 | 11
  2761. 13 | 13
  2762. 15 | 15
  2763. 17 | 17
  2764. 19 | 19
  2765. 21 | 21
  2766. 23 | 23
  2767. 25 | 25
  2768. 27 | 27
  2769. 29 | 29
  2770. 31 | 31
  2771. 33 | 33
  2772. 35 | 35
  2773. 37 | 37
  2774. 39 | 39
  2775. 41 | 41
  2776. 43 | 43
  2777. 45 | 45
  2778. 47 | 47
  2779. 49 | 49
  2780. (26 rows)
  2781. WITH cte (x) AS (
  2782. select 1 union all select 1 union all select 1 union all
  2783. SELECT * FROM generate_series(5, 49, 2)
  2784. )
  2785. SELECT x, (sum(x) over w)
  2786. FROM cte
  2787. WINDOW w AS (ORDER BY x groups between 1 preceding and 1 following);
  2788. x | sum
  2789. ----+-----
  2790. 1 | 8
  2791. 1 | 8
  2792. 1 | 8
  2793. 5 | 15
  2794. 7 | 21
  2795. 9 | 27
  2796. 11 | 33
  2797. 13 | 39
  2798. 15 | 45
  2799. 17 | 51
  2800. 19 | 57
  2801. 21 | 63
  2802. 23 | 69
  2803. 25 | 75
  2804. 27 | 81
  2805. 29 | 87
  2806. 31 | 93
  2807. 33 | 99
  2808. 35 | 105
  2809. 37 | 111
  2810. 39 | 117
  2811. 41 | 123
  2812. 43 | 129
  2813. 45 | 135
  2814. 47 | 141
  2815. 49 | 96
  2816. (26 rows)
  2817. -- with UNION
  2818. SELECT count(*) OVER (PARTITION BY four) FROM (SELECT * FROM tenk1 UNION ALL SELECT * FROM tenk2)s LIMIT 0;
  2819. count
  2820. -------
  2821. (0 rows)
  2822. -- check some degenerate cases
  2823. create temp table t1 (f1 int, f2 int8);
  2824. insert into t1 values (1,1),(1,2),(2,2);
  2825. select f1, sum(f1) over (partition by f1
  2826. range between 1 preceding and 1 following)
  2827. from t1 where f1 = f2; -- error, must have order by
  2828. ERROR: RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column
  2829. LINE 1: select f1, sum(f1) over (partition by f1
  2830. ^
  2831. explain (costs off)
  2832. select f1, sum(f1) over (partition by f1 order by f2
  2833. range between 1 preceding and 1 following)
  2834. from t1 where f1 = f2;
  2835. QUERY PLAN
  2836. ---------------------------------
  2837. WindowAgg
  2838. -> Sort
  2839. Sort Key: f1
  2840. -> Seq Scan on t1
  2841. Filter: (f1 = f2)
  2842. (5 rows)
  2843. select f1, sum(f1) over (partition by f1 order by f2
  2844. range between 1 preceding and 1 following)
  2845. from t1 where f1 = f2;
  2846. f1 | sum
  2847. ----+-----
  2848. 1 | 1
  2849. 2 | 2
  2850. (2 rows)
  2851. select f1, sum(f1) over (partition by f1, f1 order by f2
  2852. range between 2 preceding and 1 preceding)
  2853. from t1 where f1 = f2;
  2854. f1 | sum
  2855. ----+-----
  2856. 1 |
  2857. 2 |
  2858. (2 rows)
  2859. select f1, sum(f1) over (partition by f1, f2 order by f2
  2860. range between 1 following and 2 following)
  2861. from t1 where f1 = f2;
  2862. f1 | sum
  2863. ----+-----
  2864. 1 |
  2865. 2 |
  2866. (2 rows)
  2867. select f1, sum(f1) over (partition by f1
  2868. groups between 1 preceding and 1 following)
  2869. from t1 where f1 = f2; -- error, must have order by
  2870. ERROR: GROUPS mode requires an ORDER BY clause
  2871. LINE 1: select f1, sum(f1) over (partition by f1
  2872. ^
  2873. explain (costs off)
  2874. select f1, sum(f1) over (partition by f1 order by f2
  2875. groups between 1 preceding and 1 following)
  2876. from t1 where f1 = f2;
  2877. QUERY PLAN
  2878. ---------------------------------
  2879. WindowAgg
  2880. -> Sort
  2881. Sort Key: f1
  2882. -> Seq Scan on t1
  2883. Filter: (f1 = f2)
  2884. (5 rows)
  2885. select f1, sum(f1) over (partition by f1 order by f2
  2886. groups between 1 preceding and 1 following)
  2887. from t1 where f1 = f2;
  2888. f1 | sum
  2889. ----+-----
  2890. 1 | 1
  2891. 2 | 2
  2892. (2 rows)
  2893. select f1, sum(f1) over (partition by f1, f1 order by f2
  2894. groups between 2 preceding and 1 preceding)
  2895. from t1 where f1 = f2;
  2896. f1 | sum
  2897. ----+-----
  2898. 1 |
  2899. 2 |
  2900. (2 rows)
  2901. select f1, sum(f1) over (partition by f1, f2 order by f2
  2902. groups between 1 following and 2 following)
  2903. from t1 where f1 = f2;
  2904. f1 | sum
  2905. ----+-----
  2906. 1 |
  2907. 2 |
  2908. (2 rows)
  2909. -- ordering by a non-integer constant is allowed
  2910. SELECT rank() OVER (ORDER BY length('abc'));
  2911. rank
  2912. ------
  2913. 1
  2914. (1 row)
  2915. -- can't order by another window function
  2916. SELECT rank() OVER (ORDER BY rank() OVER (ORDER BY random()));
  2917. ERROR: window functions are not allowed in window definitions
  2918. LINE 1: SELECT rank() OVER (ORDER BY rank() OVER (ORDER BY random())...
  2919. ^
  2920. -- some other errors
  2921. SELECT * FROM empsalary WHERE row_number() OVER (ORDER BY salary) < 10;
  2922. ERROR: window functions are not allowed in WHERE
  2923. LINE 1: SELECT * FROM empsalary WHERE row_number() OVER (ORDER BY sa...
  2924. ^
  2925. SELECT * FROM empsalary INNER JOIN tenk1 ON row_number() OVER (ORDER BY salary) < 10;
  2926. ERROR: window functions are not allowed in JOIN conditions
  2927. LINE 1: SELECT * FROM empsalary INNER JOIN tenk1 ON row_number() OVE...
  2928. ^
  2929. SELECT rank() OVER (ORDER BY 1), count(*) FROM empsalary GROUP BY 1;
  2930. ERROR: window functions are not allowed in GROUP BY
  2931. LINE 1: SELECT rank() OVER (ORDER BY 1), count(*) FROM empsalary GRO...
  2932. ^
  2933. SELECT * FROM rank() OVER (ORDER BY random());
  2934. ERROR: syntax error at or near "ORDER"
  2935. LINE 1: SELECT * FROM rank() OVER (ORDER BY random());
  2936. ^
  2937. DELETE FROM empsalary WHERE (rank() OVER (ORDER BY random())) > 10;
  2938. ERROR: window functions are not allowed in WHERE
  2939. LINE 1: DELETE FROM empsalary WHERE (rank() OVER (ORDER BY random())...
  2940. ^
  2941. DELETE FROM empsalary RETURNING rank() OVER (ORDER BY random());
  2942. ERROR: window functions are not allowed in RETURNING
  2943. LINE 1: DELETE FROM empsalary RETURNING rank() OVER (ORDER BY random...
  2944. ^
  2945. SELECT count(*) OVER w FROM tenk1 WINDOW w AS (ORDER BY unique1), w AS (ORDER BY unique1);
  2946. ERROR: window "w" is already defined
  2947. LINE 1: ...w FROM tenk1 WINDOW w AS (ORDER BY unique1), w AS (ORDER BY ...
  2948. ^
  2949. SELECT rank() OVER (PARTITION BY four, ORDER BY ten) FROM tenk1;
  2950. ERROR: syntax error at or near "ORDER"
  2951. LINE 1: SELECT rank() OVER (PARTITION BY four, ORDER BY ten) FROM te...
  2952. ^
  2953. SELECT count() OVER () FROM tenk1;
  2954. ERROR: count(*) must be used to call a parameterless aggregate function
  2955. LINE 1: SELECT count() OVER () FROM tenk1;
  2956. ^
  2957. SELECT generate_series(1, 100) OVER () FROM empsalary;
  2958. ERROR: OVER specified, but generate_series is not a window function nor an aggregate function
  2959. LINE 1: SELECT generate_series(1, 100) OVER () FROM empsalary;
  2960. ^
  2961. SELECT ntile(0) OVER (ORDER BY ten), ten, four FROM tenk1;
  2962. ERROR: argument of ntile must be greater than zero
  2963. SELECT nth_value(four, 0) OVER (ORDER BY ten), ten, four FROM tenk1;
  2964. ERROR: argument of nth_value must be greater than zero
  2965. -- filter
  2966. SELECT sum(salary), row_number() OVER (ORDER BY depname), sum(
  2967. sum(salary) FILTER (WHERE enroll_date > '2007-01-01')
  2968. ) FILTER (WHERE depname <> 'sales') OVER (ORDER BY depname DESC) AS "filtered_sum",
  2969. depname
  2970. FROM empsalary GROUP BY depname;
  2971. sum | row_number | filtered_sum | depname
  2972. -------+------------+--------------+-----------
  2973. 25100 | 1 | 22600 | develop
  2974. 7400 | 2 | 3500 | personnel
  2975. 14600 | 3 | | sales
  2976. (3 rows)
  2977. -- Test pushdown of quals into a subquery containing window functions
  2978. -- pushdown is safe because all PARTITION BY clauses include depname:
  2979. EXPLAIN (COSTS OFF)
  2980. SELECT * FROM
  2981. (SELECT depname,
  2982. sum(salary) OVER (PARTITION BY depname) depsalary,
  2983. min(salary) OVER (PARTITION BY depname || 'A', depname) depminsalary
  2984. FROM empsalary) emp
  2985. WHERE depname = 'sales';
  2986. QUERY PLAN
  2987. --------------------------------------------------------------------------
  2988. Subquery Scan on emp
  2989. -> WindowAgg
  2990. -> WindowAgg
  2991. -> Sort
  2992. Sort Key: (((empsalary.depname)::text || 'A'::text))
  2993. -> Seq Scan on empsalary
  2994. Filter: ((depname)::text = 'sales'::text)
  2995. (7 rows)
  2996. -- pushdown is unsafe because there's a PARTITION BY clause without depname:
  2997. EXPLAIN (COSTS OFF)
  2998. SELECT * FROM
  2999. (SELECT depname,
  3000. sum(salary) OVER (PARTITION BY enroll_date) enroll_salary,
  3001. min(salary) OVER (PARTITION BY depname) depminsalary
  3002. FROM empsalary) emp
  3003. WHERE depname = 'sales';
  3004. QUERY PLAN
  3005. -------------------------------------------------------
  3006. Subquery Scan on emp
  3007. Filter: ((emp.depname)::text = 'sales'::text)
  3008. -> WindowAgg
  3009. -> Sort
  3010. Sort Key: empsalary.enroll_date
  3011. -> WindowAgg
  3012. -> Sort
  3013. Sort Key: empsalary.depname
  3014. -> Seq Scan on empsalary
  3015. (9 rows)
  3016. -- Test Sort node collapsing
  3017. EXPLAIN (COSTS OFF)
  3018. SELECT * FROM
  3019. (SELECT depname,
  3020. sum(salary) OVER (PARTITION BY depname order by empno) depsalary,
  3021. min(salary) OVER (PARTITION BY depname, empno order by enroll_date) depminsalary
  3022. FROM empsalary) emp
  3023. WHERE depname = 'sales';
  3024. QUERY PLAN
  3025. ----------------------------------------------------------------------
  3026. Subquery Scan on emp
  3027. -> WindowAgg
  3028. -> WindowAgg
  3029. -> Sort
  3030. Sort Key: empsalary.empno, empsalary.enroll_date
  3031. -> Seq Scan on empsalary
  3032. Filter: ((depname)::text = 'sales'::text)
  3033. (7 rows)
  3034. -- Test Sort node reordering
  3035. EXPLAIN (COSTS OFF)
  3036. SELECT
  3037. lead(1) OVER (PARTITION BY depname ORDER BY salary, enroll_date),
  3038. lag(1) OVER (PARTITION BY depname ORDER BY salary,enroll_date,empno)
  3039. FROM empsalary;
  3040. QUERY PLAN
  3041. -------------------------------------------------------------
  3042. WindowAgg
  3043. -> WindowAgg
  3044. -> Sort
  3045. Sort Key: depname, salary, enroll_date, empno
  3046. -> Seq Scan on empsalary
  3047. (5 rows)
  3048. -- Test incremental sorting
  3049. EXPLAIN (COSTS OFF)
  3050. SELECT * FROM
  3051. (SELECT depname,
  3052. empno,
  3053. salary,
  3054. enroll_date,
  3055. row_number() OVER (PARTITION BY depname ORDER BY enroll_date) AS first_emp,
  3056. row_number() OVER (PARTITION BY depname ORDER BY enroll_date DESC) AS last_emp
  3057. FROM empsalary) emp
  3058. WHERE first_emp = 1 OR last_emp = 1;
  3059. QUERY PLAN
  3060. -----------------------------------------------------------------------------------
  3061. Subquery Scan on emp
  3062. Filter: ((emp.first_emp = 1) OR (emp.last_emp = 1))
  3063. -> WindowAgg
  3064. -> Incremental Sort
  3065. Sort Key: empsalary.depname, empsalary.enroll_date
  3066. Presorted Key: empsalary.depname
  3067. -> WindowAgg
  3068. -> Sort
  3069. Sort Key: empsalary.depname, empsalary.enroll_date DESC
  3070. -> Seq Scan on empsalary
  3071. (10 rows)
  3072. SELECT * FROM
  3073. (SELECT depname,
  3074. empno,
  3075. salary,
  3076. enroll_date,
  3077. row_number() OVER (PARTITION BY depname ORDER BY enroll_date) AS first_emp,
  3078. row_number() OVER (PARTITION BY depname ORDER BY enroll_date DESC) AS last_emp
  3079. FROM empsalary) emp
  3080. WHERE first_emp = 1 OR last_emp = 1;
  3081. depname | empno | salary | enroll_date | first_emp | last_emp
  3082. -----------+-------+--------+-------------+-----------+----------
  3083. develop | 8 | 6000 | 10-01-2006 | 1 | 5
  3084. develop | 7 | 4200 | 01-01-2008 | 5 | 1
  3085. personnel | 2 | 3900 | 12-23-2006 | 1 | 2
  3086. personnel | 5 | 3500 | 12-10-2007 | 2 | 1
  3087. sales | 1 | 5000 | 10-01-2006 | 1 | 3
  3088. sales | 4 | 4800 | 08-08-2007 | 3 | 1
  3089. (6 rows)
  3090. -- cleanup
  3091. DROP TABLE empsalary;
  3092. -- test user-defined window function with named args and default args
  3093. CREATE FUNCTION nth_value_def(val anyelement, n integer = 1) RETURNS anyelement
  3094. LANGUAGE internal WINDOW IMMUTABLE STRICT AS 'window_nth_value';
  3095. SELECT nth_value_def(n := 2, val := ten) OVER (PARTITION BY four), ten, four
  3096. FROM (SELECT * FROM tenk1 WHERE unique2 < 10 ORDER BY four, ten) s;
  3097. nth_value_def | ten | four
  3098. ---------------+-----+------
  3099. 0 | 0 | 0
  3100. 0 | 0 | 0
  3101. 0 | 4 | 0
  3102. 1 | 1 | 1
  3103. 1 | 1 | 1
  3104. 1 | 7 | 1
  3105. 1 | 9 | 1
  3106. | 0 | 2
  3107. 3 | 1 | 3
  3108. 3 | 3 | 3
  3109. (10 rows)
  3110. SELECT nth_value_def(ten) OVER (PARTITION BY four), ten, four
  3111. FROM (SELECT * FROM tenk1 WHERE unique2 < 10 ORDER BY four, ten) s;
  3112. nth_value_def | ten | four
  3113. ---------------+-----+------
  3114. 0 | 0 | 0
  3115. 0 | 0 | 0
  3116. 0 | 4 | 0
  3117. 1 | 1 | 1
  3118. 1 | 1 | 1
  3119. 1 | 7 | 1
  3120. 1 | 9 | 1
  3121. 0 | 0 | 2
  3122. 1 | 1 | 3
  3123. 1 | 3 | 3
  3124. (10 rows)
  3125. --
  3126. -- Test the basic moving-aggregate machinery
  3127. --
  3128. -- create aggregates that record the series of transform calls (these are
  3129. -- intentionally not true inverses)
  3130. CREATE FUNCTION logging_sfunc_nonstrict(text, anyelement) RETURNS text AS
  3131. $$ SELECT COALESCE($1, '') || '*' || quote_nullable($2) $$
  3132. LANGUAGE SQL IMMUTABLE;
  3133. CREATE FUNCTION logging_msfunc_nonstrict(text, anyelement) RETURNS text AS
  3134. $$ SELECT COALESCE($1, '') || '+' || quote_nullable($2) $$
  3135. LANGUAGE SQL IMMUTABLE;
  3136. CREATE FUNCTION logging_minvfunc_nonstrict(text, anyelement) RETURNS text AS
  3137. $$ SELECT $1 || '-' || quote_nullable($2) $$
  3138. LANGUAGE SQL IMMUTABLE;
  3139. CREATE AGGREGATE logging_agg_nonstrict (anyelement)
  3140. (
  3141. stype = text,
  3142. sfunc = logging_sfunc_nonstrict,
  3143. mstype = text,
  3144. msfunc = logging_msfunc_nonstrict,
  3145. minvfunc = logging_minvfunc_nonstrict
  3146. );
  3147. CREATE AGGREGATE logging_agg_nonstrict_initcond (anyelement)
  3148. (
  3149. stype = text,
  3150. sfunc = logging_sfunc_nonstrict,
  3151. mstype = text,
  3152. msfunc = logging_msfunc_nonstrict,
  3153. minvfunc = logging_minvfunc_nonstrict,
  3154. initcond = 'I',
  3155. minitcond = 'MI'
  3156. );
  3157. CREATE FUNCTION logging_sfunc_strict(text, anyelement) RETURNS text AS
  3158. $$ SELECT $1 || '*' || quote_nullable($2) $$
  3159. LANGUAGE SQL STRICT IMMUTABLE;
  3160. CREATE FUNCTION logging_msfunc_strict(text, anyelement) RETURNS text AS
  3161. $$ SELECT $1 || '+' || quote_nullable($2) $$
  3162. LANGUAGE SQL STRICT IMMUTABLE;
  3163. CREATE FUNCTION logging_minvfunc_strict(text, anyelement) RETURNS text AS
  3164. $$ SELECT $1 || '-' || quote_nullable($2) $$
  3165. LANGUAGE SQL STRICT IMMUTABLE;
  3166. CREATE AGGREGATE logging_agg_strict (text)
  3167. (
  3168. stype = text,
  3169. sfunc = logging_sfunc_strict,
  3170. mstype = text,
  3171. msfunc = logging_msfunc_strict,
  3172. minvfunc = logging_minvfunc_strict
  3173. );
  3174. CREATE AGGREGATE logging_agg_strict_initcond (anyelement)
  3175. (
  3176. stype = text,
  3177. sfunc = logging_sfunc_strict,
  3178. mstype = text,
  3179. msfunc = logging_msfunc_strict,
  3180. minvfunc = logging_minvfunc_strict,
  3181. initcond = 'I',
  3182. minitcond = 'MI'
  3183. );
  3184. -- test strict and non-strict cases
  3185. SELECT
  3186. p::text || ',' || i::text || ':' || COALESCE(v::text, 'NULL') AS row,
  3187. logging_agg_nonstrict(v) over wnd as nstrict,
  3188. logging_agg_nonstrict_initcond(v) over wnd as nstrict_init,
  3189. logging_agg_strict(v::text) over wnd as strict,
  3190. logging_agg_strict_initcond(v) over wnd as strict_init
  3191. FROM (VALUES
  3192. (1, 1, NULL),
  3193. (1, 2, 'a'),
  3194. (1, 3, 'b'),
  3195. (1, 4, NULL),
  3196. (1, 5, NULL),
  3197. (1, 6, 'c'),
  3198. (2, 1, NULL),
  3199. (2, 2, 'x'),
  3200. (3, 1, 'z')
  3201. ) AS t(p, i, v)
  3202. WINDOW wnd AS (PARTITION BY P ORDER BY i ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
  3203. ORDER BY p, i;
  3204. row | nstrict | nstrict_init | strict | strict_init
  3205. ----------+-----------------------------------------------+-------------------------------------------------+-----------+----------------
  3206. 1,1:NULL | +NULL | MI+NULL | | MI
  3207. 1,2:a | +NULL+'a' | MI+NULL+'a' | a | MI+'a'
  3208. 1,3:b | +NULL+'a'-NULL+'b' | MI+NULL+'a'-NULL+'b' | a+'b' | MI+'a'+'b'
  3209. 1,4:NULL | +NULL+'a'-NULL+'b'-'a'+NULL | MI+NULL+'a'-NULL+'b'-'a'+NULL | a+'b'-'a' | MI+'a'+'b'-'a'
  3210. 1,5:NULL | +NULL+'a'-NULL+'b'-'a'+NULL-'b'+NULL | MI+NULL+'a'-NULL+'b'-'a'+NULL-'b'+NULL | | MI
  3211. 1,6:c | +NULL+'a'-NULL+'b'-'a'+NULL-'b'+NULL-NULL+'c' | MI+NULL+'a'-NULL+'b'-'a'+NULL-'b'+NULL-NULL+'c' | c | MI+'c'
  3212. 2,1:NULL | +NULL | MI+NULL | | MI
  3213. 2,2:x | +NULL+'x' | MI+NULL+'x' | x | MI+'x'
  3214. 3,1:z | +'z' | MI+'z' | z | MI+'z'
  3215. (9 rows)
  3216. -- and again, but with filter
  3217. SELECT
  3218. p::text || ',' || i::text || ':' ||
  3219. CASE WHEN f THEN COALESCE(v::text, 'NULL') ELSE '-' END as row,
  3220. logging_agg_nonstrict(v) filter(where f) over wnd as nstrict_filt,
  3221. logging_agg_nonstrict_initcond(v) filter(where f) over wnd as nstrict_init_filt,
  3222. logging_agg_strict(v::text) filter(where f) over wnd as strict_filt,
  3223. logging_agg_strict_initcond(v) filter(where f) over wnd as strict_init_filt
  3224. FROM (VALUES
  3225. (1, 1, true, NULL),
  3226. (1, 2, false, 'a'),
  3227. (1, 3, true, 'b'),
  3228. (1, 4, false, NULL),
  3229. (1, 5, false, NULL),
  3230. (1, 6, false, 'c'),
  3231. (2, 1, false, NULL),
  3232. (2, 2, true, 'x'),
  3233. (3, 1, true, 'z')
  3234. ) AS t(p, i, f, v)
  3235. WINDOW wnd AS (PARTITION BY p ORDER BY i ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
  3236. ORDER BY p, i;
  3237. row | nstrict_filt | nstrict_init_filt | strict_filt | strict_init_filt
  3238. ----------+--------------+-------------------+-------------+------------------
  3239. 1,1:NULL | +NULL | MI+NULL | | MI
  3240. 1,2:- | +NULL | MI+NULL | | MI
  3241. 1,3:b | +'b' | MI+'b' | b | MI+'b'
  3242. 1,4:- | +'b' | MI+'b' | b | MI+'b'
  3243. 1,5:- | | MI | | MI
  3244. 1,6:- | | MI | | MI
  3245. 2,1:- | | MI | | MI
  3246. 2,2:x | +'x' | MI+'x' | x | MI+'x'
  3247. 3,1:z | +'z' | MI+'z' | z | MI+'z'
  3248. (9 rows)
  3249. -- test that volatile arguments disable moving-aggregate mode
  3250. SELECT
  3251. i::text || ':' || COALESCE(v::text, 'NULL') as row,
  3252. logging_agg_strict(v::text)
  3253. over wnd as inverse,
  3254. logging_agg_strict(v::text || CASE WHEN random() < 0 then '?' ELSE '' END)
  3255. over wnd as noinverse
  3256. FROM (VALUES
  3257. (1, 'a'),
  3258. (2, 'b'),
  3259. (3, 'c')
  3260. ) AS t(i, v)
  3261. WINDOW wnd AS (ORDER BY i ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
  3262. ORDER BY i;
  3263. row | inverse | noinverse
  3264. -----+---------------+-----------
  3265. 1:a | a | a
  3266. 2:b | a+'b' | a*'b'
  3267. 3:c | a+'b'-'a'+'c' | b*'c'
  3268. (3 rows)
  3269. SELECT
  3270. i::text || ':' || COALESCE(v::text, 'NULL') as row,
  3271. logging_agg_strict(v::text) filter(where true)
  3272. over wnd as inverse,
  3273. logging_agg_strict(v::text) filter(where random() >= 0)
  3274. over wnd as noinverse
  3275. FROM (VALUES
  3276. (1, 'a'),
  3277. (2, 'b'),
  3278. (3, 'c')
  3279. ) AS t(i, v)
  3280. WINDOW wnd AS (ORDER BY i ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
  3281. ORDER BY i;
  3282. row | inverse | noinverse
  3283. -----+---------------+-----------
  3284. 1:a | a | a
  3285. 2:b | a+'b' | a*'b'
  3286. 3:c | a+'b'-'a'+'c' | b*'c'
  3287. (3 rows)
  3288. -- test that non-overlapping windows don't use inverse transitions
  3289. SELECT
  3290. logging_agg_strict(v::text) OVER wnd
  3291. FROM (VALUES
  3292. (1, 'a'),
  3293. (2, 'b'),
  3294. (3, 'c')
  3295. ) AS t(i, v)
  3296. WINDOW wnd AS (ORDER BY i ROWS BETWEEN CURRENT ROW AND CURRENT ROW)
  3297. ORDER BY i;
  3298. logging_agg_strict
  3299. --------------------
  3300. a
  3301. b
  3302. c
  3303. (3 rows)
  3304. -- test that returning NULL from the inverse transition functions
  3305. -- restarts the aggregation from scratch. The second aggregate is supposed
  3306. -- to test cases where only some aggregates restart, the third one checks
  3307. -- that one aggregate restarting doesn't cause others to restart.
  3308. CREATE FUNCTION sum_int_randrestart_minvfunc(int4, int4) RETURNS int4 AS
  3309. $$ SELECT CASE WHEN random() < 0.2 THEN NULL ELSE $1 - $2 END $$
  3310. LANGUAGE SQL STRICT;
  3311. CREATE AGGREGATE sum_int_randomrestart (int4)
  3312. (
  3313. stype = int4,
  3314. sfunc = int4pl,
  3315. mstype = int4,
  3316. msfunc = int4pl,
  3317. minvfunc = sum_int_randrestart_minvfunc
  3318. );
  3319. WITH
  3320. vs AS (
  3321. SELECT i, (random() * 100)::int4 AS v
  3322. FROM generate_series(1, 100) AS i
  3323. ),
  3324. sum_following AS (
  3325. SELECT i, SUM(v) OVER
  3326. (ORDER BY i DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS s
  3327. FROM vs
  3328. )
  3329. SELECT DISTINCT
  3330. sum_following.s = sum_int_randomrestart(v) OVER fwd AS eq1,
  3331. -sum_following.s = sum_int_randomrestart(-v) OVER fwd AS eq2,
  3332. 100*3+(vs.i-1)*3 = length(logging_agg_nonstrict(''::text) OVER fwd) AS eq3
  3333. FROM vs
  3334. JOIN sum_following ON sum_following.i = vs.i
  3335. WINDOW fwd AS (
  3336. ORDER BY vs.i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
  3337. );
  3338. eq1 | eq2 | eq3
  3339. -----+-----+-----
  3340. t | t | t
  3341. (1 row)
  3342. --
  3343. -- Test various built-in aggregates that have moving-aggregate support
  3344. --
  3345. -- test inverse transition functions handle NULLs properly
  3346. SELECT i,AVG(v::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3347. FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
  3348. i | avg
  3349. ---+--------------------
  3350. 1 | 1.5000000000000000
  3351. 2 | 2.0000000000000000
  3352. 3 |
  3353. 4 |
  3354. (4 rows)
  3355. SELECT i,AVG(v::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3356. FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
  3357. i | avg
  3358. ---+--------------------
  3359. 1 | 1.5000000000000000
  3360. 2 | 2.0000000000000000
  3361. 3 |
  3362. 4 |
  3363. (4 rows)
  3364. SELECT i,AVG(v::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3365. FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
  3366. i | avg
  3367. ---+--------------------
  3368. 1 | 1.5000000000000000
  3369. 2 | 2.0000000000000000
  3370. 3 |
  3371. 4 |
  3372. (4 rows)
  3373. SELECT i,AVG(v::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3374. FROM (VALUES(1,1.5),(2,2.5),(3,NULL),(4,NULL)) t(i,v);
  3375. i | avg
  3376. ---+--------------------
  3377. 1 | 2.0000000000000000
  3378. 2 | 2.5000000000000000
  3379. 3 |
  3380. 4 |
  3381. (4 rows)
  3382. SELECT i,AVG(v::interval) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3383. FROM (VALUES(1,'1 sec'),(2,'2 sec'),(3,NULL),(4,NULL)) t(i,v);
  3384. i | avg
  3385. ---+------------
  3386. 1 | @ 1.5 secs
  3387. 2 | @ 2 secs
  3388. 3 |
  3389. 4 |
  3390. (4 rows)
  3391. SELECT i,SUM(v::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3392. FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
  3393. i | sum
  3394. ---+-----
  3395. 1 | 3
  3396. 2 | 2
  3397. 3 |
  3398. 4 |
  3399. (4 rows)
  3400. SELECT i,SUM(v::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3401. FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
  3402. i | sum
  3403. ---+-----
  3404. 1 | 3
  3405. 2 | 2
  3406. 3 |
  3407. 4 |
  3408. (4 rows)
  3409. SELECT i,SUM(v::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3410. FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
  3411. i | sum
  3412. ---+-----
  3413. 1 | 3
  3414. 2 | 2
  3415. 3 |
  3416. 4 |
  3417. (4 rows)
  3418. SELECT i,SUM(v::money) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3419. FROM (VALUES(1,'1.10'),(2,'2.20'),(3,NULL),(4,NULL)) t(i,v);
  3420. i | sum
  3421. ---+-------
  3422. 1 | $3.30
  3423. 2 | $2.20
  3424. 3 |
  3425. 4 |
  3426. (4 rows)
  3427. SELECT i,SUM(v::interval) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3428. FROM (VALUES(1,'1 sec'),(2,'2 sec'),(3,NULL),(4,NULL)) t(i,v);
  3429. i | sum
  3430. ---+----------
  3431. 1 | @ 3 secs
  3432. 2 | @ 2 secs
  3433. 3 |
  3434. 4 |
  3435. (4 rows)
  3436. SELECT i,SUM(v::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3437. FROM (VALUES(1,1.1),(2,2.2),(3,NULL),(4,NULL)) t(i,v);
  3438. i | sum
  3439. ---+-----
  3440. 1 | 3.3
  3441. 2 | 2.2
  3442. 3 |
  3443. 4 |
  3444. (4 rows)
  3445. SELECT SUM(n::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3446. FROM (VALUES(1,1.01),(2,2),(3,3)) v(i,n);
  3447. sum
  3448. ------
  3449. 6.01
  3450. 5
  3451. 3
  3452. (3 rows)
  3453. SELECT i,COUNT(v) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3454. FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
  3455. i | count
  3456. ---+-------
  3457. 1 | 2
  3458. 2 | 1
  3459. 3 | 0
  3460. 4 | 0
  3461. (4 rows)
  3462. SELECT i,COUNT(*) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3463. FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
  3464. i | count
  3465. ---+-------
  3466. 1 | 4
  3467. 2 | 3
  3468. 3 | 2
  3469. 4 | 1
  3470. (4 rows)
  3471. SELECT VAR_POP(n::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3472. FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3473. var_pop
  3474. -----------------------
  3475. 21704.000000000000
  3476. 13868.750000000000
  3477. 11266.666666666667
  3478. 4225.0000000000000000
  3479. 0
  3480. (5 rows)
  3481. SELECT VAR_POP(n::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3482. FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3483. var_pop
  3484. -----------------------
  3485. 21704.000000000000
  3486. 13868.750000000000
  3487. 11266.666666666667
  3488. 4225.0000000000000000
  3489. 0
  3490. (5 rows)
  3491. SELECT VAR_POP(n::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3492. FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3493. var_pop
  3494. -----------------------
  3495. 21704.000000000000
  3496. 13868.750000000000
  3497. 11266.666666666667
  3498. 4225.0000000000000000
  3499. 0
  3500. (5 rows)
  3501. SELECT VAR_POP(n::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3502. FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3503. var_pop
  3504. -----------------------
  3505. 21704.000000000000
  3506. 13868.750000000000
  3507. 11266.666666666667
  3508. 4225.0000000000000000
  3509. 0
  3510. (5 rows)
  3511. SELECT VAR_SAMP(n::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3512. FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3513. var_samp
  3514. -----------------------
  3515. 27130.000000000000
  3516. 18491.666666666667
  3517. 16900.000000000000
  3518. 8450.0000000000000000
  3519. (5 rows)
  3520. SELECT VAR_SAMP(n::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3521. FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3522. var_samp
  3523. -----------------------
  3524. 27130.000000000000
  3525. 18491.666666666667
  3526. 16900.000000000000
  3527. 8450.0000000000000000
  3528. (5 rows)
  3529. SELECT VAR_SAMP(n::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3530. FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3531. var_samp
  3532. -----------------------
  3533. 27130.000000000000
  3534. 18491.666666666667
  3535. 16900.000000000000
  3536. 8450.0000000000000000
  3537. (5 rows)
  3538. SELECT VAR_SAMP(n::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3539. FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3540. var_samp
  3541. -----------------------
  3542. 27130.000000000000
  3543. 18491.666666666667
  3544. 16900.000000000000
  3545. 8450.0000000000000000
  3546. (5 rows)
  3547. SELECT VARIANCE(n::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3548. FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3549. variance
  3550. -----------------------
  3551. 27130.000000000000
  3552. 18491.666666666667
  3553. 16900.000000000000
  3554. 8450.0000000000000000
  3555. (5 rows)
  3556. SELECT VARIANCE(n::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3557. FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3558. variance
  3559. -----------------------
  3560. 27130.000000000000
  3561. 18491.666666666667
  3562. 16900.000000000000
  3563. 8450.0000000000000000
  3564. (5 rows)
  3565. SELECT VARIANCE(n::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3566. FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3567. variance
  3568. -----------------------
  3569. 27130.000000000000
  3570. 18491.666666666667
  3571. 16900.000000000000
  3572. 8450.0000000000000000
  3573. (5 rows)
  3574. SELECT VARIANCE(n::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3575. FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3576. variance
  3577. -----------------------
  3578. 27130.000000000000
  3579. 18491.666666666667
  3580. 16900.000000000000
  3581. 8450.0000000000000000
  3582. (5 rows)
  3583. SELECT STDDEV_POP(n::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3584. FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
  3585. stddev_pop
  3586. ---------------------
  3587. 147.322774885623
  3588. 147.322774885623
  3589. 117.765657133139
  3590. 106.144555520604
  3591. 65.0000000000000000
  3592. 0
  3593. (6 rows)
  3594. SELECT STDDEV_POP(n::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3595. FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
  3596. stddev_pop
  3597. ---------------------
  3598. 147.322774885623
  3599. 147.322774885623
  3600. 117.765657133139
  3601. 106.144555520604
  3602. 65.0000000000000000
  3603. 0
  3604. (6 rows)
  3605. SELECT STDDEV_POP(n::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3606. FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
  3607. stddev_pop
  3608. ---------------------
  3609. 147.322774885623
  3610. 147.322774885623
  3611. 117.765657133139
  3612. 106.144555520604
  3613. 65.0000000000000000
  3614. 0
  3615. (6 rows)
  3616. SELECT STDDEV_POP(n::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3617. FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
  3618. stddev_pop
  3619. ---------------------
  3620. 147.322774885623
  3621. 147.322774885623
  3622. 117.765657133139
  3623. 106.144555520604
  3624. 65.0000000000000000
  3625. 0
  3626. (6 rows)
  3627. SELECT STDDEV_SAMP(n::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3628. FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
  3629. stddev_samp
  3630. ---------------------
  3631. 164.711869639076
  3632. 164.711869639076
  3633. 135.984067694222
  3634. 130.000000000000
  3635. 91.9238815542511782
  3636. (6 rows)
  3637. SELECT STDDEV_SAMP(n::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3638. FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
  3639. stddev_samp
  3640. ---------------------
  3641. 164.711869639076
  3642. 164.711869639076
  3643. 135.984067694222
  3644. 130.000000000000
  3645. 91.9238815542511782
  3646. (6 rows)
  3647. SELECT STDDEV_SAMP(n::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3648. FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
  3649. stddev_samp
  3650. ---------------------
  3651. 164.711869639076
  3652. 164.711869639076
  3653. 135.984067694222
  3654. 130.000000000000
  3655. 91.9238815542511782
  3656. (6 rows)
  3657. SELECT STDDEV_SAMP(n::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3658. FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
  3659. stddev_samp
  3660. ---------------------
  3661. 164.711869639076
  3662. 164.711869639076
  3663. 135.984067694222
  3664. 130.000000000000
  3665. 91.9238815542511782
  3666. (6 rows)
  3667. SELECT STDDEV(n::bigint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3668. FROM (VALUES(0,NULL),(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3669. stddev
  3670. ---------------------
  3671. 164.711869639076
  3672. 164.711869639076
  3673. 135.984067694222
  3674. 130.000000000000
  3675. 91.9238815542511782
  3676. (6 rows)
  3677. SELECT STDDEV(n::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3678. FROM (VALUES(0,NULL),(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3679. stddev
  3680. ---------------------
  3681. 164.711869639076
  3682. 164.711869639076
  3683. 135.984067694222
  3684. 130.000000000000
  3685. 91.9238815542511782
  3686. (6 rows)
  3687. SELECT STDDEV(n::smallint) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3688. FROM (VALUES(0,NULL),(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3689. stddev
  3690. ---------------------
  3691. 164.711869639076
  3692. 164.711869639076
  3693. 135.984067694222
  3694. 130.000000000000
  3695. 91.9238815542511782
  3696. (6 rows)
  3697. SELECT STDDEV(n::numeric) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
  3698. FROM (VALUES(0,NULL),(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
  3699. stddev
  3700. ---------------------
  3701. 164.711869639076
  3702. 164.711869639076
  3703. 135.984067694222
  3704. 130.000000000000
  3705. 91.9238815542511782
  3706. (6 rows)
  3707. -- test that inverse transition functions work with various frame options
  3708. SELECT i,SUM(v::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND CURRENT ROW)
  3709. FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
  3710. i | sum
  3711. ---+-----
  3712. 1 | 1
  3713. 2 | 2
  3714. 3 |
  3715. 4 |
  3716. (4 rows)
  3717. SELECT i,SUM(v::int) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING)
  3718. FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
  3719. i | sum
  3720. ---+-----
  3721. 1 | 3
  3722. 2 | 2
  3723. 3 |
  3724. 4 |
  3725. (4 rows)
  3726. SELECT i,SUM(v::int) OVER (ORDER BY i ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
  3727. FROM (VALUES(1,1),(2,2),(3,3),(4,4)) t(i,v);
  3728. i | sum
  3729. ---+-----
  3730. 1 | 3
  3731. 2 | 6
  3732. 3 | 9
  3733. 4 | 7
  3734. (4 rows)
  3735. -- ensure aggregate over numeric properly recovers from NaN values
  3736. SELECT a, b,
  3737. SUM(b) OVER(ORDER BY A ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
  3738. FROM (VALUES(1,1::numeric),(2,2),(3,'NaN'),(4,3),(5,4)) t(a,b);
  3739. a | b | sum
  3740. ---+-----+-----
  3741. 1 | 1 | 1
  3742. 2 | 2 | 3
  3743. 3 | NaN | NaN
  3744. 4 | 3 | NaN
  3745. 5 | 4 | 7
  3746. (5 rows)
  3747. -- It might be tempting for someone to add an inverse trans function for
  3748. -- float and double precision. This should not be done as it can give incorrect
  3749. -- results. This test should fail if anyone ever does this without thinking too
  3750. -- hard about it.
  3751. SELECT to_char(SUM(n::float8) OVER (ORDER BY i ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING),'999999999999999999999D9')
  3752. FROM (VALUES(1,1e20),(2,1)) n(i,n);
  3753. to_char
  3754. --------------------------
  3755. 100000000000000000000
  3756. 1.0
  3757. (2 rows)
  3758. SELECT i, b, bool_and(b) OVER w, bool_or(b) OVER w
  3759. FROM (VALUES (1,true), (2,true), (3,false), (4,false), (5,true)) v(i,b)
  3760. WINDOW w AS (ORDER BY i ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING);
  3761. i | b | bool_and | bool_or
  3762. ---+---+----------+---------
  3763. 1 | t | t | t
  3764. 2 | t | f | t
  3765. 3 | f | f | f
  3766. 4 | f | f | t
  3767. 5 | t | t | t
  3768. (5 rows)
  3769. -- Tests for problems with failure to walk or mutate expressions
  3770. -- within window frame clauses.
  3771. -- test walker (fails with collation error if expressions are not walked)
  3772. SELECT array_agg(i) OVER w
  3773. FROM generate_series(1,5) i
  3774. WINDOW w AS (ORDER BY i ROWS BETWEEN (('foo' < 'foobar')::integer) PRECEDING AND CURRENT ROW);
  3775. array_agg
  3776. -----------
  3777. {1}
  3778. {1,2}
  3779. {2,3}
  3780. {3,4}
  3781. {4,5}
  3782. (5 rows)
  3783. -- test mutator (fails when inlined if expressions are not mutated)
  3784. CREATE FUNCTION pg_temp.f(group_size BIGINT) RETURNS SETOF integer[]
  3785. AS $$
  3786. SELECT array_agg(s) OVER w
  3787. FROM generate_series(1,5) s
  3788. WINDOW w AS (ORDER BY s ROWS BETWEEN CURRENT ROW AND GROUP_SIZE FOLLOWING)
  3789. $$ LANGUAGE SQL STABLE;
  3790. EXPLAIN (costs off) SELECT * FROM pg_temp.f(2);
  3791. QUERY PLAN
  3792. ------------------------------------------------------
  3793. Subquery Scan on f
  3794. -> WindowAgg
  3795. -> Sort
  3796. Sort Key: s.s
  3797. -> Function Scan on generate_series s
  3798. (5 rows)
  3799. SELECT * FROM pg_temp.f(2);
  3800. f
  3801. ---------
  3802. {1,2,3}
  3803. {2,3,4}
  3804. {3,4,5}
  3805. {4,5}
  3806. {5}
  3807. (5 rows)