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.

140 lines
7.7KB

  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>2.7. 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="tutorial-join.html" title="2.6. Joins Between Tables" /><link rel="next" href="tutorial-update.html" title="2.8. Updates" /></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">2.7. Aggregate Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="tutorial-join.html" title="2.6. Joins Between Tables">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="tutorial-sql.html" title="Chapter 2. The SQL Language">Up</a></td><th width="60%" align="center">Chapter 2. The <acronym xmlns="http://www.w3.org/1999/xhtml" class="acronym">SQL</acronym> Language</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="tutorial-update.html" title="2.8. Updates">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="TUTORIAL-AGG"><div class="titlepage"><div><div><h2 class="title" style="clear: both">2.7. Aggregate Functions</h2></div></div></div><a id="id-1.4.4.8.2" class="indexterm"></a><p>
  3. Like most other relational database products,
  4. <span class="productname">PostgreSQL</span> supports
  5. <em class="firstterm">aggregate functions</em>.
  6. An aggregate function computes a single result from multiple input rows.
  7. For example, there are aggregates to compute the
  8. <code class="function">count</code>, <code class="function">sum</code>,
  9. <code class="function">avg</code> (average), <code class="function">max</code> (maximum) and
  10. <code class="function">min</code> (minimum) over a set of rows.
  11. </p><p>
  12. As an example, we can find the highest low-temperature reading anywhere
  13. with:
  14. </p><pre class="programlisting">
  15. SELECT max(temp_lo) FROM weather;
  16. </pre><p>
  17. </p><pre class="screen">
  18. max
  19. -----
  20. 46
  21. (1 row)
  22. </pre><p>
  23. </p><p>
  24. <a id="id-1.4.4.8.5.1" class="indexterm"></a>
  25. If we wanted to know what city (or cities) that reading occurred in,
  26. we might try:
  27. </p><pre class="programlisting">
  28. SELECT city FROM weather WHERE temp_lo = max(temp_lo); <em class="lineannotation"><span class="lineannotation">WRONG</span></em>
  29. </pre><p>
  30. but this will not work since the aggregate
  31. <code class="function">max</code> cannot be used in the
  32. <code class="literal">WHERE</code> clause. (This restriction exists because
  33. the <code class="literal">WHERE</code> clause determines which rows will be
  34. included in the aggregate calculation; so obviously it has to be evaluated
  35. before aggregate functions are computed.)
  36. However, as is often the case
  37. the query can be restated to accomplish the desired result, here
  38. by using a <em class="firstterm">subquery</em>:
  39. </p><pre class="programlisting">
  40. SELECT city FROM weather
  41. WHERE temp_lo = (SELECT max(temp_lo) FROM weather);
  42. </pre><p>
  43. </p><pre class="screen">
  44. city
  45. ---------------
  46. San Francisco
  47. (1 row)
  48. </pre><p>
  49. This is OK because the subquery is an independent computation
  50. that computes its own aggregate separately from what is happening
  51. in the outer query.
  52. </p><p>
  53. <a id="id-1.4.4.8.6.1" class="indexterm"></a>
  54. <a id="id-1.4.4.8.6.2" class="indexterm"></a>
  55. Aggregates are also very useful in combination with <code class="literal">GROUP
  56. BY</code> clauses. For example, we can get the maximum low
  57. temperature observed in each city with:
  58. </p><pre class="programlisting">
  59. SELECT city, max(temp_lo)
  60. FROM weather
  61. GROUP BY city;
  62. </pre><p>
  63. </p><pre class="screen">
  64. city | max
  65. ---------------+-----
  66. Hayward | 37
  67. San Francisco | 46
  68. (2 rows)
  69. </pre><p>
  70. which gives us one output row per city. Each aggregate result is
  71. computed over the table rows matching that city.
  72. We can filter these grouped
  73. rows using <code class="literal">HAVING</code>:
  74. </p><pre class="programlisting">
  75. SELECT city, max(temp_lo)
  76. FROM weather
  77. GROUP BY city
  78. HAVING max(temp_lo) &lt; 40;
  79. </pre><p>
  80. </p><pre class="screen">
  81. city | max
  82. ---------+-----
  83. Hayward | 37
  84. (1 row)
  85. </pre><p>
  86. which gives us the same results for only the cities that have all
  87. <code class="structfield">temp_lo</code> values below 40. Finally, if we only care about
  88. cities whose
  89. names begin with <span class="quote">“<span class="quote"><code class="literal">S</code></span>”</span>, we might do:
  90. </p><pre class="programlisting">
  91. SELECT city, max(temp_lo)
  92. FROM weather
  93. WHERE city LIKE 'S%' -- <span id="co.tutorial-agg-like"></span>(1)
  94. GROUP BY city
  95. HAVING max(temp_lo) &lt; 40;
  96. </pre><p>
  97. </p><div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><p><a href="#co.tutorial-agg-like">(1)</a> </p></td><td valign="top" align="left"><p>
  98. The <code class="literal">LIKE</code> operator does pattern matching and
  99. is explained in <a class="xref" href="functions-matching.html" title="9.7. Pattern Matching">Section 9.7</a>.
  100. </p></td></tr></table></div><p>
  101. </p><p>
  102. It is important to understand the interaction between aggregates and
  103. <acronym class="acronym">SQL</acronym>'s <code class="literal">WHERE</code> and <code class="literal">HAVING</code> clauses.
  104. The fundamental difference between <code class="literal">WHERE</code> and
  105. <code class="literal">HAVING</code> is this: <code class="literal">WHERE</code> selects
  106. input rows before groups and aggregates are computed (thus, it controls
  107. which rows go into the aggregate computation), whereas
  108. <code class="literal">HAVING</code> selects group rows after groups and
  109. aggregates are computed. Thus, the
  110. <code class="literal">WHERE</code> clause must not contain aggregate functions;
  111. it makes no sense to try to use an aggregate to determine which rows
  112. will be inputs to the aggregates. On the other hand, the
  113. <code class="literal">HAVING</code> clause always contains aggregate functions.
  114. (Strictly speaking, you are allowed to write a <code class="literal">HAVING</code>
  115. clause that doesn't use aggregates, but it's seldom useful. The same
  116. condition could be used more efficiently at the <code class="literal">WHERE</code>
  117. stage.)
  118. </p><p>
  119. In the previous example, we can apply the city name restriction in
  120. <code class="literal">WHERE</code>, since it needs no aggregate. This is
  121. more efficient than adding the restriction to <code class="literal">HAVING</code>,
  122. because we avoid doing the grouping and aggregate calculations
  123. for all rows that fail the <code class="literal">WHERE</code> check.
  124. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="tutorial-join.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="tutorial-sql.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="tutorial-update.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">2.6. Joins Between Tables </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 2.8. Updates</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1