2
0

lang_aggfunc.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html><head>
  3. <title>SQLite Query Language: Aggregate Functions</title>
  4. <style type="text/css">
  5. body {
  6. margin: auto;
  7. font-family: Verdana, sans-serif;
  8. padding: 8px 1%;
  9. }
  10. a { color: #45735f }
  11. a:visited { color: #734559 }
  12. .logo { position:absolute; margin:3px; }
  13. .tagline {
  14. float:right;
  15. text-align:right;
  16. font-style:italic;
  17. width:240px;
  18. margin:12px;
  19. margin-top:58px;
  20. }
  21. .toolbar {
  22. font-variant: small-caps;
  23. text-align: center;
  24. line-height: 1.6em;
  25. margin: 0;
  26. padding:1px 8px;
  27. }
  28. .toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
  29. .toolbar a:visited { color: white; }
  30. .toolbar a:hover { color: #80a796; background: white; }
  31. .content { margin: 5%; }
  32. .content dt { font-weight:bold; }
  33. .content dd { margin-bottom: 25px; margin-left:20%; }
  34. .content ul { padding:0px; padding-left: 15px; margin:0px; }
  35. /* rounded corners */
  36. .se { background: url(images/se.png) 100% 100% no-repeat #80a796}
  37. .sw { background: url(images/sw.png) 0% 100% no-repeat }
  38. .ne { background: url(images/ne.png) 100% 0% no-repeat }
  39. .nw { background: url(images/nw.png) 0% 0% no-repeat }
  40. </style>
  41. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  42. </head>
  43. <body>
  44. <div><!-- container div to satisfy validator -->
  45. <a href="lang.html">
  46. <h2 align="center">SQL As Understood By SQLite</h2></a><h1>Aggregate Functions</h1>
  47. <p>
  48. The aggregate functions shown below are available by default. </p>
  49. <p>
  50. In any aggregate function that takes a single argument, that argument
  51. can be preceded by the keyword DISTINCT. In such cases, duplicate
  52. elements are filtered before being passed into the aggregate function.
  53. For example, the function "count(distinct X)" will return the number
  54. of distinct values of column X instead of the total number of non-null
  55. values in column X.
  56. </p>
  57. <table border=0 cellpadding=10>
  58. <tr><td valign="top" align="right" width="120">avg(<i>X</i>)</td><td valign="top"><a name="avg"></a>
  59. Return the average value of all non-NULL <i>X</i> within a
  60. group. String and BLOB values that do not look like numbers are
  61. interpreted as 0.
  62. The result of avg() is always a floating point value as long as
  63. at there is at least one non-NULL input even if all
  64. inputs are integers. The result of avg() is NULL if and only if
  65. there are no non-NULL inputs.
  66. </td></tr><tr><td valign="top" align="right" width="120">count(<i>X</i>)<br></br>count(*)</td><td valign="top"><a name="count"></a>
  67. The first form return a count of the number of times
  68. that <i>X</i> is not NULL in a group. The second form (with no argument)
  69. returns the total number of rows in the group.
  70. </td></tr><tr><td valign="top" align="right" width="120">group_concat(<i>X</i>)<br></br>group_concat(<i>X</i>,<i>Y</i>)</td><td valign="top"><a name="group_concat"></a>
  71. The result is a string which is the concatenation of
  72. all non-NULL values of <i>X</i>. If parameter <i>Y</i> is present then
  73. is is used as the separator
  74. between instances of <i>X</i>. A comma (",") is used as the separator
  75. if <i>Y</i> is omitted. The order of the concatenated elements is
  76. arbitrary.
  77. </td></tr><tr><td valign="top" align="right" width="120">max(<i>X</i>)</td><td valign="top"><a name="maxAggFunc"></a>
  78. Return the maximum value of all values in the group.
  79. The maximum value is the value that would be returned last in an
  80. ORDER BY on the same column. NULL is returned if and only if there are
  81. no non-NULL values in the group.
  82. </td></tr><tr><td valign="top" align="right" width="120">min(<i>X</i>)</td><td valign="top"><a name="minAggFunc"></a>
  83. Return the minimum non-NULL value of all values in the group.
  84. The minimum value is the first non-NULL value that would appear
  85. in an ORDER BY of the column.
  86. NULL is only returned if and only if there are no non-NULL values in the
  87. group.
  88. </td></tr><tr><td valign="top" align="right" width="120">sum(<i>X</i>)<br></br>total(<i>X</i>)</td><td valign="top"><a name="sumFunc"></a>
  89. Return the numeric sum of all non-NULL values in the group.
  90. If there are no non-NULL input rows then sum() returns
  91. NULL but total() returns 0.0.
  92. NULL is not normally a helpful result for the sum of no rows
  93. but the SQL standard requires it and most other
  94. SQL database engines implement sum() that way so SQLite does it in the
  95. same way in order to be compatible. The non-standard total() function
  96. is provided as a convenient way to work around this design problem
  97. in the SQL language.</p>
  98. <p>The result of total() is always a floating point value.
  99. The result of sum() is an integer value if all non-NULL inputs are integers.
  100. If any input to sum() is neither an integer or a NULL
  101. then sum() returns a floating point value
  102. which might be an approximation to the true sum.</p>
  103. <p>Sum() will throw an "integer overflow" exception if all inputs
  104. are integers or NULL
  105. and an integer overflow occurs at any point during the computation.
  106. Total() never throws an integer overflow.
  107. </td></tr>
  108. </table>