gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

563 lines
39KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>9.20. Aggregate Functions</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="functions-range.html" title="9.19. Range Functions and Operators" /><link rel="next" href="functions-window.html" title="9.21. Window Functions" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">9.20. Aggregate Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-range.html" title="9.19. Range Functions and Operators">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><th width="60%" align="center">Chapter 9. Functions and Operators</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="functions-window.html" title="9.21. Window Functions">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="FUNCTIONS-AGGREGATE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.20. Aggregate Functions</h2></div></div></div><a id="id-1.5.8.25.2" class="indexterm"></a><p>
  3. <em class="firstterm">Aggregate functions</em> compute a single result
  4. from a set of input values. The built-in general-purpose aggregate
  5. functions are listed in <a class="xref" href="functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE" title="Table 9.55. General-Purpose Aggregate Functions">Table 9.55</a>
  6. and statistical aggregates in <a class="xref" href="functions-aggregate.html#FUNCTIONS-AGGREGATE-STATISTICS-TABLE" title="Table 9.56. Aggregate Functions for Statistics">Table 9.56</a>.
  7. The built-in within-group ordered-set aggregate functions
  8. are listed in <a class="xref" href="functions-aggregate.html#FUNCTIONS-ORDEREDSET-TABLE" title="Table 9.57. Ordered-Set Aggregate Functions">Table 9.57</a>
  9. while the built-in within-group hypothetical-set ones are in <a class="xref" href="functions-aggregate.html#FUNCTIONS-HYPOTHETICAL-TABLE" title="Table 9.58. Hypothetical-Set Aggregate Functions">Table 9.58</a>. Grouping operations,
  10. which are closely related to aggregate functions, are listed in
  11. <a class="xref" href="functions-aggregate.html#FUNCTIONS-GROUPING-TABLE" title="Table 9.59. Grouping Operations">Table 9.59</a>.
  12. The special syntax considerations for aggregate
  13. functions are explained in <a class="xref" href="sql-expressions.html#SYNTAX-AGGREGATES" title="4.2.7. Aggregate Expressions">Section 4.2.7</a>.
  14. Consult <a class="xref" href="tutorial-agg.html" title="2.7. Aggregate Functions">Section 2.7</a> for additional introductory
  15. information.
  16. </p><div class="table" id="FUNCTIONS-AGGREGATE-TABLE"><p class="title"><strong>Table 9.55. General-Purpose Aggregate Functions</strong></p><div class="table-contents"><table class="table" summary="General-Purpose Aggregate Functions" border="1"><colgroup><col /><col /><col /><col /><col /></colgroup><thead><tr><th>Function</th><th>Argument Type(s)</th><th>Return Type</th><th>Partial Mode</th><th>Description</th></tr></thead><tbody><tr><td>
  17. <a id="id-1.5.8.25.4.2.2.1.1.1" class="indexterm"></a>
  18. <code class="function">array_agg(<em class="replaceable"><code>expression</code></em>)</code>
  19. </td><td>
  20. any non-array type
  21. </td><td>
  22. array of the argument type
  23. </td><td>No</td><td>input values, including nulls, concatenated into an array</td></tr><tr><td>
  24. <code class="function">array_agg(<em class="replaceable"><code>expression</code></em>)</code>
  25. </td><td>
  26. any array type
  27. </td><td>
  28. same as argument data type
  29. </td><td>No</td><td>input arrays concatenated into array of one higher dimension
  30. (inputs must all have same dimensionality,
  31. and cannot be empty or null)</td></tr><tr><td>
  32. <a id="id-1.5.8.25.4.2.2.3.1.1" class="indexterm"></a>
  33. <a id="id-1.5.8.25.4.2.2.3.1.2" class="indexterm"></a>
  34. <code class="function">avg(<em class="replaceable"><code>expression</code></em>)</code>
  35. </td><td>
  36. <code class="type">smallint</code>, <code class="type">int</code>,
  37. <code class="type">bigint</code>, <code class="type">real</code>, <code class="type">double
  38. precision</code>, <code class="type">numeric</code>, or <code class="type">interval</code>
  39. </td><td>
  40. <code class="type">numeric</code> for any integer-type argument,
  41. <code class="type">double precision</code> for a floating-point argument,
  42. otherwise the same as the argument data type
  43. </td><td>Yes</td><td>the average (arithmetic mean) of all non-null input values</td></tr><tr><td>
  44. <a id="id-1.5.8.25.4.2.2.4.1.1" class="indexterm"></a>
  45. <code class="function">bit_and(<em class="replaceable"><code>expression</code></em>)</code>
  46. </td><td>
  47. <code class="type">smallint</code>, <code class="type">int</code>, <code class="type">bigint</code>, or
  48. <code class="type">bit</code>
  49. </td><td>
  50. same as argument data type
  51. </td><td>Yes</td><td>the bitwise AND of all non-null input values, or null if none</td></tr><tr><td>
  52. <a id="id-1.5.8.25.4.2.2.5.1.1" class="indexterm"></a>
  53. <code class="function">bit_or(<em class="replaceable"><code>expression</code></em>)</code>
  54. </td><td>
  55. <code class="type">smallint</code>, <code class="type">int</code>, <code class="type">bigint</code>, or
  56. <code class="type">bit</code>
  57. </td><td>
  58. same as argument data type
  59. </td><td>Yes</td><td>the bitwise OR of all non-null input values, or null if none</td></tr><tr><td>
  60. <a id="id-1.5.8.25.4.2.2.6.1.1" class="indexterm"></a>
  61. <code class="function">bool_and(<em class="replaceable"><code>expression</code></em>)</code>
  62. </td><td>
  63. <code class="type">bool</code>
  64. </td><td>
  65. <code class="type">bool</code>
  66. </td><td>Yes</td><td>true if all input values are true, otherwise false</td></tr><tr><td>
  67. <a id="id-1.5.8.25.4.2.2.7.1.1" class="indexterm"></a>
  68. <code class="function">bool_or(<em class="replaceable"><code>expression</code></em>)</code>
  69. </td><td>
  70. <code class="type">bool</code>
  71. </td><td>
  72. <code class="type">bool</code>
  73. </td><td>Yes</td><td>true if at least one input value is true, otherwise false</td></tr><tr><td>
  74. <a id="id-1.5.8.25.4.2.2.8.1.1" class="indexterm"></a>
  75. <code class="function">count(*)</code>
  76. </td><td> </td><td><code class="type">bigint</code></td><td>Yes</td><td>number of input rows</td></tr><tr><td><code class="function">count(<em class="replaceable"><code>expression</code></em>)</code></td><td>any</td><td><code class="type">bigint</code></td><td>Yes</td><td>
  77. number of input rows for which the value of <em class="replaceable"><code>expression</code></em> is not null
  78. </td></tr><tr><td>
  79. <a id="id-1.5.8.25.4.2.2.10.1.1" class="indexterm"></a>
  80. <code class="function">every(<em class="replaceable"><code>expression</code></em>)</code>
  81. </td><td>
  82. <code class="type">bool</code>
  83. </td><td>
  84. <code class="type">bool</code>
  85. </td><td>Yes</td><td>equivalent to <code class="function">bool_and</code></td></tr><tr><td>
  86. <a id="id-1.5.8.25.4.2.2.11.1.1" class="indexterm"></a>
  87. <code class="function">json_agg(<em class="replaceable"><code>expression</code></em>)</code>
  88. </td><td>
  89. <code class="type">any</code>
  90. </td><td>
  91. <code class="type">json</code>
  92. </td><td>No</td><td>aggregates values, including nulls, as a JSON array</td></tr><tr><td>
  93. <a id="id-1.5.8.25.4.2.2.12.1.1" class="indexterm"></a>
  94. <code class="function">jsonb_agg(<em class="replaceable"><code>expression</code></em>)</code>
  95. </td><td>
  96. <code class="type">any</code>
  97. </td><td>
  98. <code class="type">jsonb</code>
  99. </td><td>No</td><td>aggregates values, including nulls, as a JSON array</td></tr><tr><td>
  100. <a id="id-1.5.8.25.4.2.2.13.1.1" class="indexterm"></a>
  101. <code class="function">json_object_agg(<em class="replaceable"><code>name</code></em>, <em class="replaceable"><code>value</code></em>)</code>
  102. </td><td>
  103. <code class="type">(any, any)</code>
  104. </td><td>
  105. <code class="type">json</code>
  106. </td><td>No</td><td>aggregates name/value pairs as a JSON object; values can be
  107. null, but not names</td></tr><tr><td>
  108. <a id="id-1.5.8.25.4.2.2.14.1.1" class="indexterm"></a>
  109. <code class="function">jsonb_object_agg(<em class="replaceable"><code>name</code></em>, <em class="replaceable"><code>value</code></em>)</code>
  110. </td><td>
  111. <code class="type">(any, any)</code>
  112. </td><td>
  113. <code class="type">jsonb</code>
  114. </td><td>No</td><td>aggregates name/value pairs as a JSON object; values can be
  115. null, but not names</td></tr><tr><td>
  116. <a id="id-1.5.8.25.4.2.2.15.1.1" class="indexterm"></a>
  117. <code class="function">max(<em class="replaceable"><code>expression</code></em>)</code>
  118. </td><td>any numeric, string, date/time, network, or enum type,
  119. or arrays of these types</td><td>same as argument type</td><td>Yes</td><td>
  120. maximum value of <em class="replaceable"><code>expression</code></em> across all non-null input
  121. values
  122. </td></tr><tr><td>
  123. <a id="id-1.5.8.25.4.2.2.16.1.1" class="indexterm"></a>
  124. <code class="function">min(<em class="replaceable"><code>expression</code></em>)</code>
  125. </td><td>any numeric, string, date/time, network, or enum type,
  126. or arrays of these types</td><td>same as argument type</td><td>Yes</td><td>
  127. minimum value of <em class="replaceable"><code>expression</code></em> across all non-null input
  128. values
  129. </td></tr><tr><td>
  130. <a id="id-1.5.8.25.4.2.2.17.1.1" class="indexterm"></a>
  131. <code class="function">
  132. string_agg(<em class="replaceable"><code>expression</code></em>,
  133. <em class="replaceable"><code>delimiter</code></em>)
  134. </code>
  135. </td><td>
  136. (<code class="type">text</code>, <code class="type">text</code>) or (<code class="type">bytea</code>, <code class="type">bytea</code>)
  137. </td><td>
  138. same as argument types
  139. </td><td>No</td><td>non-null input values concatenated into a string, separated by delimiter</td></tr><tr><td>
  140. <a id="id-1.5.8.25.4.2.2.18.1.1" class="indexterm"></a>
  141. <code class="function">sum(<em class="replaceable"><code>expression</code></em>)</code>
  142. </td><td>
  143. <code class="type">smallint</code>, <code class="type">int</code>,
  144. <code class="type">bigint</code>, <code class="type">real</code>, <code class="type">double
  145. precision</code>, <code class="type">numeric</code>,
  146. <code class="type">interval</code>, or <code class="type">money</code>
  147. </td><td>
  148. <code class="type">bigint</code> for <code class="type">smallint</code> or
  149. <code class="type">int</code> arguments, <code class="type">numeric</code> for
  150. <code class="type">bigint</code> arguments, otherwise the same as the
  151. argument data type
  152. </td><td>Yes</td><td>sum of <em class="replaceable"><code>expression</code></em>
  153. across all non-null input values</td></tr><tr><td>
  154. <a id="id-1.5.8.25.4.2.2.19.1.1" class="indexterm"></a>
  155. <code class="function">xmlagg(<em class="replaceable"><code>expression</code></em>)</code>
  156. </td><td>
  157. <code class="type">xml</code>
  158. </td><td>
  159. <code class="type">xml</code>
  160. </td><td>No</td><td>concatenation of non-null XML values
  161. (see also <a class="xref" href="functions-xml.html#FUNCTIONS-XML-XMLAGG" title="9.14.1.7. xmlagg">Section 9.14.1.7</a>)</td></tr></tbody></table></div></div><br class="table-break" /><p>
  162. It should be noted that except for <code class="function">count</code>,
  163. these functions return a null value when no rows are selected. In
  164. particular, <code class="function">sum</code> of no rows returns null, not
  165. zero as one might expect, and <code class="function">array_agg</code>
  166. returns null rather than an empty array when there are no input
  167. rows. The <code class="function">coalesce</code> function can be used to
  168. substitute zero or an empty array for null when necessary.
  169. </p><p>
  170. Aggregate functions which support <em class="firstterm">Partial Mode</em>
  171. are eligible to participate in various optimizations, such as parallel
  172. aggregation.
  173. </p><div class="note"><h3 class="title">Note</h3><a id="id-1.5.8.25.7.1" class="indexterm"></a><a id="id-1.5.8.25.7.2" class="indexterm"></a><p>
  174. Boolean aggregates <code class="function">bool_and</code> and
  175. <code class="function">bool_or</code> correspond to standard SQL aggregates
  176. <code class="function">every</code> and <code class="function">any</code> or
  177. <code class="function">some</code>.
  178. As for <code class="function">any</code> and <code class="function">some</code>,
  179. it seems that there is an ambiguity built into the standard syntax:
  180. </p><pre class="programlisting">
  181. SELECT b1 = ANY((SELECT b2 FROM t2 ...)) FROM t1 ...;
  182. </pre><p>
  183. Here <code class="function">ANY</code> can be considered either as introducing
  184. a subquery, or as being an aggregate function, if the subquery
  185. returns one row with a Boolean value.
  186. Thus the standard name cannot be given to these aggregates.
  187. </p></div><div class="note"><h3 class="title">Note</h3><p>
  188. Users accustomed to working with other SQL database management
  189. systems might be disappointed by the performance of the
  190. <code class="function">count</code> aggregate when it is applied to the
  191. entire table. A query like:
  192. </p><pre class="programlisting">
  193. SELECT count(*) FROM sometable;
  194. </pre><p>
  195. will require effort proportional to the size of the table:
  196. <span class="productname">PostgreSQL</span> will need to scan either the
  197. entire table or the entirety of an index which includes all rows in
  198. the table.
  199. </p></div><p>
  200. The aggregate functions <code class="function">array_agg</code>,
  201. <code class="function">json_agg</code>, <code class="function">jsonb_agg</code>,
  202. <code class="function">json_object_agg</code>, <code class="function">jsonb_object_agg</code>,
  203. <code class="function">string_agg</code>,
  204. and <code class="function">xmlagg</code>, as well as similar user-defined
  205. aggregate functions, produce meaningfully different result values
  206. depending on the order of the input values. This ordering is
  207. unspecified by default, but can be controlled by writing an
  208. <code class="literal">ORDER BY</code> clause within the aggregate call, as shown in
  209. <a class="xref" href="sql-expressions.html#SYNTAX-AGGREGATES" title="4.2.7. Aggregate Expressions">Section 4.2.7</a>.
  210. Alternatively, supplying the input values from a sorted subquery
  211. will usually work. For example:
  212. </p><pre class="screen">
  213. SELECT xmlagg(x) FROM (SELECT x FROM test ORDER BY y DESC) AS tab;
  214. </pre><p>
  215. Beware that this approach can fail if the outer query level contains
  216. additional processing, such as a join, because that might cause the
  217. subquery's output to be reordered before the aggregate is computed.
  218. </p><p>
  219. <a class="xref" href="functions-aggregate.html#FUNCTIONS-AGGREGATE-STATISTICS-TABLE" title="Table 9.56. Aggregate Functions for Statistics">Table 9.56</a> shows
  220. aggregate functions typically used in statistical analysis.
  221. (These are separated out merely to avoid cluttering the listing
  222. of more-commonly-used aggregates.) Where the description mentions
  223. <em class="replaceable"><code>N</code></em>, it means the
  224. number of input rows for which all the input expressions are non-null.
  225. In all cases, null is returned if the computation is meaningless,
  226. for example when <em class="replaceable"><code>N</code></em> is zero.
  227. </p><a id="id-1.5.8.25.11" class="indexterm"></a><a id="id-1.5.8.25.12" class="indexterm"></a><div class="table" id="FUNCTIONS-AGGREGATE-STATISTICS-TABLE"><p class="title"><strong>Table 9.56. Aggregate Functions for Statistics</strong></p><div class="table-contents"><table class="table" summary="Aggregate Functions for Statistics" border="1"><colgroup><col /><col /><col /><col /><col /></colgroup><thead><tr><th>Function</th><th>Argument Type</th><th>Return Type</th><th>Partial Mode</th><th>Description</th></tr></thead><tbody><tr><td>
  228. <a id="id-1.5.8.25.13.2.2.1.1.1" class="indexterm"></a>
  229. <a id="id-1.5.8.25.13.2.2.1.1.2" class="indexterm"></a>
  230. <code class="function">corr(<em class="replaceable"><code>Y</code></em>, <em class="replaceable"><code>X</code></em>)</code>
  231. </td><td>
  232. <code class="type">double precision</code>
  233. </td><td>
  234. <code class="type">double precision</code>
  235. </td><td>Yes</td><td>correlation coefficient</td></tr><tr><td>
  236. <a id="id-1.5.8.25.13.2.2.2.1.1" class="indexterm"></a>
  237. <a id="id-1.5.8.25.13.2.2.2.1.2" class="indexterm"></a>
  238. <code class="function">covar_pop(<em class="replaceable"><code>Y</code></em>, <em class="replaceable"><code>X</code></em>)</code>
  239. </td><td>
  240. <code class="type">double precision</code>
  241. </td><td>
  242. <code class="type">double precision</code>
  243. </td><td>Yes</td><td>population covariance</td></tr><tr><td>
  244. <a id="id-1.5.8.25.13.2.2.3.1.1" class="indexterm"></a>
  245. <a id="id-1.5.8.25.13.2.2.3.1.2" class="indexterm"></a>
  246. <code class="function">covar_samp(<em class="replaceable"><code>Y</code></em>, <em class="replaceable"><code>X</code></em>)</code>
  247. </td><td>
  248. <code class="type">double precision</code>
  249. </td><td>
  250. <code class="type">double precision</code>
  251. </td><td>Yes</td><td>sample covariance</td></tr><tr><td>
  252. <a id="id-1.5.8.25.13.2.2.4.1.1" class="indexterm"></a>
  253. <code class="function">regr_avgx(<em class="replaceable"><code>Y</code></em>, <em class="replaceable"><code>X</code></em>)</code>
  254. </td><td>
  255. <code class="type">double precision</code>
  256. </td><td>
  257. <code class="type">double precision</code>
  258. </td><td>Yes</td><td>average of the independent variable
  259. (<code class="literal">sum(<em class="replaceable"><code>X</code></em>)/<em class="replaceable"><code>N</code></em></code>)</td></tr><tr><td>
  260. <a id="id-1.5.8.25.13.2.2.5.1.1" class="indexterm"></a>
  261. <code class="function">regr_avgy(<em class="replaceable"><code>Y</code></em>, <em class="replaceable"><code>X</code></em>)</code>
  262. </td><td>
  263. <code class="type">double precision</code>
  264. </td><td>
  265. <code class="type">double precision</code>
  266. </td><td>Yes</td><td>average of the dependent variable
  267. (<code class="literal">sum(<em class="replaceable"><code>Y</code></em>)/<em class="replaceable"><code>N</code></em></code>)</td></tr><tr><td>
  268. <a id="id-1.5.8.25.13.2.2.6.1.1" class="indexterm"></a>
  269. <code class="function">regr_count(<em class="replaceable"><code>Y</code></em>, <em class="replaceable"><code>X</code></em>)</code>
  270. </td><td>
  271. <code class="type">double precision</code>
  272. </td><td>
  273. <code class="type">bigint</code>
  274. </td><td>Yes</td><td>number of input rows in which both expressions are nonnull</td></tr><tr><td>
  275. <a id="id-1.5.8.25.13.2.2.7.1.1" class="indexterm"></a>
  276. <a id="id-1.5.8.25.13.2.2.7.1.2" class="indexterm"></a>
  277. <code class="function">regr_intercept(<em class="replaceable"><code>Y</code></em>, <em class="replaceable"><code>X</code></em>)</code>
  278. </td><td>
  279. <code class="type">double precision</code>
  280. </td><td>
  281. <code class="type">double precision</code>
  282. </td><td>Yes</td><td>y-intercept of the least-squares-fit linear equation
  283. determined by the (<em class="replaceable"><code>X</code></em>, <em class="replaceable"><code>Y</code></em>) pairs</td></tr><tr><td>
  284. <a id="id-1.5.8.25.13.2.2.8.1.1" class="indexterm"></a>
  285. <code class="function">regr_r2(<em class="replaceable"><code>Y</code></em>, <em class="replaceable"><code>X</code></em>)</code>
  286. </td><td>
  287. <code class="type">double precision</code>
  288. </td><td>
  289. <code class="type">double precision</code>
  290. </td><td>Yes</td><td>square of the correlation coefficient</td></tr><tr><td>
  291. <a id="id-1.5.8.25.13.2.2.9.1.1" class="indexterm"></a>
  292. <a id="id-1.5.8.25.13.2.2.9.1.2" class="indexterm"></a>
  293. <code class="function">regr_slope(<em class="replaceable"><code>Y</code></em>, <em class="replaceable"><code>X</code></em>)</code>
  294. </td><td>
  295. <code class="type">double precision</code>
  296. </td><td>
  297. <code class="type">double precision</code>
  298. </td><td>Yes</td><td>slope of the least-squares-fit linear equation determined
  299. by the (<em class="replaceable"><code>X</code></em>,
  300. <em class="replaceable"><code>Y</code></em>) pairs</td></tr><tr><td>
  301. <a id="id-1.5.8.25.13.2.2.10.1.1" class="indexterm"></a>
  302. <code class="function">regr_sxx(<em class="replaceable"><code>Y</code></em>, <em class="replaceable"><code>X</code></em>)</code>
  303. </td><td>
  304. <code class="type">double precision</code>
  305. </td><td>
  306. <code class="type">double precision</code>
  307. </td><td>Yes</td><td><code class="literal">sum(<em class="replaceable"><code>X</code></em>^2) - sum(<em class="replaceable"><code>X</code></em>)^2/<em class="replaceable"><code>N</code></em></code> (<span class="quote">“<span class="quote">sum of
  308. squares</span>”</span> of the independent variable)</td></tr><tr><td>
  309. <a id="id-1.5.8.25.13.2.2.11.1.1" class="indexterm"></a>
  310. <code class="function">regr_sxy(<em class="replaceable"><code>Y</code></em>, <em class="replaceable"><code>X</code></em>)</code>
  311. </td><td>
  312. <code class="type">double precision</code>
  313. </td><td>
  314. <code class="type">double precision</code>
  315. </td><td>Yes</td><td><code class="literal">sum(<em class="replaceable"><code>X</code></em>*<em class="replaceable"><code>Y</code></em>) - sum(<em class="replaceable"><code>X</code></em>) * sum(<em class="replaceable"><code>Y</code></em>)/<em class="replaceable"><code>N</code></em></code> (<span class="quote">“<span class="quote">sum of
  316. products</span>”</span> of independent times dependent
  317. variable)</td></tr><tr><td>
  318. <a id="id-1.5.8.25.13.2.2.12.1.1" class="indexterm"></a>
  319. <code class="function">regr_syy(<em class="replaceable"><code>Y</code></em>, <em class="replaceable"><code>X</code></em>)</code>
  320. </td><td>
  321. <code class="type">double precision</code>
  322. </td><td>
  323. <code class="type">double precision</code>
  324. </td><td>Yes</td><td><code class="literal">sum(<em class="replaceable"><code>Y</code></em>^2) - sum(<em class="replaceable"><code>Y</code></em>)^2/<em class="replaceable"><code>N</code></em></code> (<span class="quote">“<span class="quote">sum of
  325. squares</span>”</span> of the dependent variable)</td></tr><tr><td>
  326. <a id="id-1.5.8.25.13.2.2.13.1.1" class="indexterm"></a>
  327. <a id="id-1.5.8.25.13.2.2.13.1.2" class="indexterm"></a>
  328. <code class="function">stddev(<em class="replaceable"><code>expression</code></em>)</code>
  329. </td><td>
  330. <code class="type">smallint</code>, <code class="type">int</code>,
  331. <code class="type">bigint</code>, <code class="type">real</code>, <code class="type">double
  332. precision</code>, or <code class="type">numeric</code>
  333. </td><td>
  334. <code class="type">double precision</code> for floating-point arguments,
  335. otherwise <code class="type">numeric</code>
  336. </td><td>Yes</td><td>historical alias for <code class="function">stddev_samp</code></td></tr><tr><td>
  337. <a id="id-1.5.8.25.13.2.2.14.1.1" class="indexterm"></a>
  338. <a id="id-1.5.8.25.13.2.2.14.1.2" class="indexterm"></a>
  339. <code class="function">stddev_pop(<em class="replaceable"><code>expression</code></em>)</code>
  340. </td><td>
  341. <code class="type">smallint</code>, <code class="type">int</code>,
  342. <code class="type">bigint</code>, <code class="type">real</code>, <code class="type">double
  343. precision</code>, or <code class="type">numeric</code>
  344. </td><td>
  345. <code class="type">double precision</code> for floating-point arguments,
  346. otherwise <code class="type">numeric</code>
  347. </td><td>Yes</td><td>population standard deviation of the input values</td></tr><tr><td>
  348. <a id="id-1.5.8.25.13.2.2.15.1.1" class="indexterm"></a>
  349. <a id="id-1.5.8.25.13.2.2.15.1.2" class="indexterm"></a>
  350. <code class="function">stddev_samp(<em class="replaceable"><code>expression</code></em>)</code>
  351. </td><td>
  352. <code class="type">smallint</code>, <code class="type">int</code>,
  353. <code class="type">bigint</code>, <code class="type">real</code>, <code class="type">double
  354. precision</code>, or <code class="type">numeric</code>
  355. </td><td>
  356. <code class="type">double precision</code> for floating-point arguments,
  357. otherwise <code class="type">numeric</code>
  358. </td><td>Yes</td><td>sample standard deviation of the input values</td></tr><tr><td>
  359. <a id="id-1.5.8.25.13.2.2.16.1.1" class="indexterm"></a>
  360. <code class="function">variance</code>(<em class="replaceable"><code>expression</code></em>)
  361. </td><td>
  362. <code class="type">smallint</code>, <code class="type">int</code>,
  363. <code class="type">bigint</code>, <code class="type">real</code>, <code class="type">double
  364. precision</code>, or <code class="type">numeric</code>
  365. </td><td>
  366. <code class="type">double precision</code> for floating-point arguments,
  367. otherwise <code class="type">numeric</code>
  368. </td><td>Yes</td><td>historical alias for <code class="function">var_samp</code></td></tr><tr><td>
  369. <a id="id-1.5.8.25.13.2.2.17.1.1" class="indexterm"></a>
  370. <a id="id-1.5.8.25.13.2.2.17.1.2" class="indexterm"></a>
  371. <code class="function">var_pop</code>(<em class="replaceable"><code>expression</code></em>)
  372. </td><td>
  373. <code class="type">smallint</code>, <code class="type">int</code>,
  374. <code class="type">bigint</code>, <code class="type">real</code>, <code class="type">double
  375. precision</code>, or <code class="type">numeric</code>
  376. </td><td>
  377. <code class="type">double precision</code> for floating-point arguments,
  378. otherwise <code class="type">numeric</code>
  379. </td><td>Yes</td><td>population variance of the input values (square of the population standard deviation)</td></tr><tr><td>
  380. <a id="id-1.5.8.25.13.2.2.18.1.1" class="indexterm"></a>
  381. <a id="id-1.5.8.25.13.2.2.18.1.2" class="indexterm"></a>
  382. <code class="function">var_samp</code>(<em class="replaceable"><code>expression</code></em>)
  383. </td><td>
  384. <code class="type">smallint</code>, <code class="type">int</code>,
  385. <code class="type">bigint</code>, <code class="type">real</code>, <code class="type">double
  386. precision</code>, or <code class="type">numeric</code>
  387. </td><td>
  388. <code class="type">double precision</code> for floating-point arguments,
  389. otherwise <code class="type">numeric</code>
  390. </td><td>Yes</td><td>sample variance of the input values (square of the sample standard deviation)</td></tr></tbody></table></div></div><br class="table-break" /><p>
  391. <a class="xref" href="functions-aggregate.html#FUNCTIONS-ORDEREDSET-TABLE" title="Table 9.57. Ordered-Set Aggregate Functions">Table 9.57</a> shows some
  392. aggregate functions that use the <em class="firstterm">ordered-set aggregate</em>
  393. syntax. These functions are sometimes referred to as <span class="quote">“<span class="quote">inverse
  394. distribution</span>”</span> functions.
  395. </p><a id="id-1.5.8.25.15" class="indexterm"></a><a id="id-1.5.8.25.16" class="indexterm"></a><div class="table" id="FUNCTIONS-ORDEREDSET-TABLE"><p class="title"><strong>Table 9.57. Ordered-Set Aggregate Functions</strong></p><div class="table-contents"><table class="table" summary="Ordered-Set Aggregate Functions" border="1"><colgroup><col /><col /><col /><col /><col /><col /></colgroup><thead><tr><th>Function</th><th>Direct Argument Type(s)</th><th>Aggregated Argument Type(s)</th><th>Return Type</th><th>Partial Mode</th><th>Description</th></tr></thead><tbody><tr><td>
  396. <a id="id-1.5.8.25.17.2.2.1.1.1" class="indexterm"></a>
  397. <code class="function">mode() WITHIN GROUP (ORDER BY <em class="replaceable"><code>sort_expression</code></em>)</code>
  398. </td><td>
  399. </td><td>
  400. any sortable type
  401. </td><td>
  402. same as sort expression
  403. </td><td>No</td><td>
  404. returns the most frequent input value (arbitrarily choosing the first
  405. one if there are multiple equally-frequent results)
  406. </td></tr><tr><td>
  407. <a id="id-1.5.8.25.17.2.2.2.1.1" class="indexterm"></a>
  408. <code class="function">percentile_cont(<em class="replaceable"><code>fraction</code></em>) WITHIN GROUP (ORDER BY <em class="replaceable"><code>sort_expression</code></em>)</code>
  409. </td><td>
  410. <code class="type">double precision</code>
  411. </td><td>
  412. <code class="type">double precision</code> or <code class="type">interval</code>
  413. </td><td>
  414. same as sort expression
  415. </td><td>No</td><td>
  416. continuous percentile: returns a value corresponding to the specified
  417. fraction in the ordering, interpolating between adjacent input items if
  418. needed
  419. </td></tr><tr><td>
  420. <code class="function">percentile_cont(<em class="replaceable"><code>fractions</code></em>) WITHIN GROUP (ORDER BY <em class="replaceable"><code>sort_expression</code></em>)</code>
  421. </td><td>
  422. <code class="type">double precision[]</code>
  423. </td><td>
  424. <code class="type">double precision</code> or <code class="type">interval</code>
  425. </td><td>
  426. array of sort expression's type
  427. </td><td>No</td><td>
  428. multiple continuous percentile: returns an array of results matching
  429. the shape of the <em class="replaceable"><code>fractions</code></em> parameter, with each
  430. non-null element replaced by the value corresponding to that percentile
  431. </td></tr><tr><td>
  432. <a id="id-1.5.8.25.17.2.2.4.1.1" class="indexterm"></a>
  433. <code class="function">percentile_disc(<em class="replaceable"><code>fraction</code></em>) WITHIN GROUP (ORDER BY <em class="replaceable"><code>sort_expression</code></em>)</code>
  434. </td><td>
  435. <code class="type">double precision</code>
  436. </td><td>
  437. any sortable type
  438. </td><td>
  439. same as sort expression
  440. </td><td>No</td><td>
  441. discrete percentile: returns the first input value whose position in
  442. the ordering equals or exceeds the specified fraction
  443. </td></tr><tr><td>
  444. <code class="function">percentile_disc(<em class="replaceable"><code>fractions</code></em>) WITHIN GROUP (ORDER BY <em class="replaceable"><code>sort_expression</code></em>)</code>
  445. </td><td>
  446. <code class="type">double precision[]</code>
  447. </td><td>
  448. any sortable type
  449. </td><td>
  450. array of sort expression's type
  451. </td><td>No</td><td>
  452. multiple discrete percentile: returns an array of results matching the
  453. shape of the <em class="replaceable"><code>fractions</code></em> parameter, with each non-null
  454. element replaced by the input value corresponding to that percentile
  455. </td></tr></tbody></table></div></div><br class="table-break" /><p>
  456. All the aggregates listed in <a class="xref" href="functions-aggregate.html#FUNCTIONS-ORDEREDSET-TABLE" title="Table 9.57. Ordered-Set Aggregate Functions">Table 9.57</a>
  457. ignore null values in their sorted input. For those that take
  458. a <em class="replaceable"><code>fraction</code></em> parameter, the fraction value must be
  459. between 0 and 1; an error is thrown if not. However, a null fraction value
  460. simply produces a null result.
  461. </p><a id="id-1.5.8.25.19" class="indexterm"></a><p>
  462. Each of the aggregates listed in
  463. <a class="xref" href="functions-aggregate.html#FUNCTIONS-HYPOTHETICAL-TABLE" title="Table 9.58. Hypothetical-Set Aggregate Functions">Table 9.58</a> is associated with a
  464. window function of the same name defined in
  465. <a class="xref" href="functions-window.html" title="9.21. Window Functions">Section 9.21</a>. In each case, the aggregate result
  466. is the value that the associated window function would have
  467. returned for the <span class="quote">“<span class="quote">hypothetical</span>”</span> row constructed from
  468. <em class="replaceable"><code>args</code></em>, if such a row had been added to the sorted
  469. group of rows computed from the <em class="replaceable"><code>sorted_args</code></em>.
  470. </p><div class="table" id="FUNCTIONS-HYPOTHETICAL-TABLE"><p class="title"><strong>Table 9.58. Hypothetical-Set Aggregate Functions</strong></p><div class="table-contents"><table class="table" summary="Hypothetical-Set Aggregate Functions" border="1"><colgroup><col /><col /><col /><col /><col /><col /></colgroup><thead><tr><th>Function</th><th>Direct Argument Type(s)</th><th>Aggregated Argument Type(s)</th><th>Return Type</th><th>Partial Mode</th><th>Description</th></tr></thead><tbody><tr><td>
  471. <a id="id-1.5.8.25.21.2.2.1.1.1" class="indexterm"></a>
  472. <code class="function">rank(<em class="replaceable"><code>args</code></em>) WITHIN GROUP (ORDER BY <em class="replaceable"><code>sorted_args</code></em>)</code>
  473. </td><td>
  474. <code class="literal">VARIADIC</code> <code class="type">"any"</code>
  475. </td><td>
  476. <code class="literal">VARIADIC</code> <code class="type">"any"</code>
  477. </td><td>
  478. <code class="type">bigint</code>
  479. </td><td>No</td><td>
  480. rank of the hypothetical row, with gaps for duplicate rows
  481. </td></tr><tr><td>
  482. <a id="id-1.5.8.25.21.2.2.2.1.1" class="indexterm"></a>
  483. <code class="function">dense_rank(<em class="replaceable"><code>args</code></em>) WITHIN GROUP (ORDER BY <em class="replaceable"><code>sorted_args</code></em>)</code>
  484. </td><td>
  485. <code class="literal">VARIADIC</code> <code class="type">"any"</code>
  486. </td><td>
  487. <code class="literal">VARIADIC</code> <code class="type">"any"</code>
  488. </td><td>
  489. <code class="type">bigint</code>
  490. </td><td>No</td><td>
  491. rank of the hypothetical row, without gaps
  492. </td></tr><tr><td>
  493. <a id="id-1.5.8.25.21.2.2.3.1.1" class="indexterm"></a>
  494. <code class="function">percent_rank(<em class="replaceable"><code>args</code></em>) WITHIN GROUP (ORDER BY <em class="replaceable"><code>sorted_args</code></em>)</code>
  495. </td><td>
  496. <code class="literal">VARIADIC</code> <code class="type">"any"</code>
  497. </td><td>
  498. <code class="literal">VARIADIC</code> <code class="type">"any"</code>
  499. </td><td>
  500. <code class="type">double precision</code>
  501. </td><td>No</td><td>
  502. relative rank of the hypothetical row, ranging from 0 to 1
  503. </td></tr><tr><td>
  504. <a id="id-1.5.8.25.21.2.2.4.1.1" class="indexterm"></a>
  505. <code class="function">cume_dist(<em class="replaceable"><code>args</code></em>) WITHIN GROUP (ORDER BY <em class="replaceable"><code>sorted_args</code></em>)</code>
  506. </td><td>
  507. <code class="literal">VARIADIC</code> <code class="type">"any"</code>
  508. </td><td>
  509. <code class="literal">VARIADIC</code> <code class="type">"any"</code>
  510. </td><td>
  511. <code class="type">double precision</code>
  512. </td><td>No</td><td>
  513. relative rank of the hypothetical row, ranging from
  514. 1/<em class="replaceable"><code>N</code></em> to 1
  515. </td></tr></tbody></table></div></div><br class="table-break" /><p>
  516. For each of these hypothetical-set aggregates, the list of direct arguments
  517. given in <em class="replaceable"><code>args</code></em> must match the number and types of
  518. the aggregated arguments given in <em class="replaceable"><code>sorted_args</code></em>.
  519. Unlike most built-in aggregates, these aggregates are not strict, that is
  520. they do not drop input rows containing nulls. Null values sort according
  521. to the rule specified in the <code class="literal">ORDER BY</code> clause.
  522. </p><div class="table" id="FUNCTIONS-GROUPING-TABLE"><p class="title"><strong>Table 9.59. Grouping Operations</strong></p><div class="table-contents"><table class="table" summary="Grouping Operations" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Function</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td>
  523. <a id="id-1.5.8.25.23.2.2.1.1.1" class="indexterm"></a>
  524. <code class="function">GROUPING(<em class="replaceable"><code>args...</code></em>)</code>
  525. </td><td>
  526. <code class="type">integer</code>
  527. </td><td>
  528. Integer bit mask indicating which arguments are not being included in the current
  529. grouping set
  530. </td></tr></tbody></table></div></div><br class="table-break" /><p>
  531. Grouping operations are used in conjunction with grouping sets (see
  532. <a class="xref" href="queries-table-expressions.html#QUERIES-GROUPING-SETS" title="7.2.4. GROUPING SETS, CUBE, and ROLLUP">Section 7.2.4</a>) to distinguish result rows. The
  533. arguments to the <code class="literal">GROUPING</code> operation are not actually evaluated,
  534. but they must match exactly expressions given in the <code class="literal">GROUP BY</code>
  535. clause of the associated query level. Bits are assigned with the rightmost
  536. argument being the least-significant bit; each bit is 0 if the corresponding
  537. expression is included in the grouping criteria of the grouping set generating
  538. the result row, and 1 if it is not. For example:
  539. </p><pre class="screen">
  540. <code class="prompt">=&gt;</code> <strong class="userinput"><code>SELECT * FROM items_sold;</code></strong>
  541. make | model | sales
  542. -------+-------+-------
  543. Foo | GT | 10
  544. Foo | Tour | 20
  545. Bar | City | 15
  546. Bar | Sport | 5
  547. (4 rows)
  548. <code class="prompt">=&gt;</code> <strong class="userinput"><code>SELECT make, model, GROUPING(make,model), sum(sales) FROM items_sold GROUP BY ROLLUP(make,model);</code></strong>
  549. make | model | grouping | sum
  550. -------+-------+----------+-----
  551. Foo | GT | 0 | 10
  552. Foo | Tour | 0 | 20
  553. Bar | City | 0 | 15
  554. Bar | Sport | 0 | 5
  555. Foo | | 1 | 30
  556. Bar | | 1 | 20
  557. | | 3 | 50
  558. (7 rows)
  559. </pre><p>
  560. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="functions-range.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="functions.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="functions-window.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.19. Range Functions and Operators </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 9.21. Window Functions</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1