当前位置:   article > 正文

PostgreSQL 聚合函数讲解 - 1 常用聚合函数

postgresql true false的聚合函数
  1. PostgreSQL支持较多的聚合函数, 以PostgreSQL 9.4为例, 支持例如一般性的聚合, 统计学科的聚合, 排序集聚合, 假象集聚合等.
  2. 本文将对一般性聚合函数举例说明其功能和用法.
  3. 聚合函数有哪些,见 :
  4. http://www.postgresql.org/docs/9.4/static/functions-aggregate.html
  5. 以上所有聚合函数, 当没有行输入时, 除了count返回0, 其他都返回null.
  6. 使用sum, array_agg时, 当没有行输入, 返回NULL可能有点别扭, 那么你可以使用coalesce来替代NULL, 如coalesce(sum(x), 0)
  7. coalesce(array_agg(x), '{}'::int[])
  8. 例子 :
  9. 聚合后得到数组, null将计入数组元素
  10. postgres=# select array_agg(id) from (values(null),(1),(2)) as t(id);
  11. array_agg
  12. ------------
  13. {NULL,1,2}
  14. (1 row)
  15. 算平均值是不计算null
  16. postgres=# select avg(id) from (values(null),(1),(2)) as t(id);
  17. avg
  18. --------------------
  19. 1.5000000000000000
  20. (1 row)
  21. bit与|或 时也不计算NULL
  22. postgres=# select bit_and(id) from (values(null),(1),(2)) as t(id);
  23. bit_and
  24. ---------
  25. 0
  26. (1 row)
  27. postgres=# select bit_or(id) from (values(null),(1),(2)) as t(id);
  28. bit_or
  29. --------
  30. 3
  31. (1 row)
  32. 算布尔逻辑时也不计算NULL
  33. postgres=# select bool_and(id) from (values(null),(true),(false)) as t(id);
  34. bool_and
  35. ----------
  36. f
  37. (1 row)
  38. every是bool_and的别名, 实际上是SQL标准中定义的.
  39. postgres=# select every(id) from (values(null),(true),(false)) as t(id);
  40. every
  41. -------
  42. f
  43. (1 row)
  44. SQL标准中还定义了any和some为bool_or的别名, 但是因为any和some还可以被解释为子查询, 所以在PostgreSQL中any和some的布尔逻辑聚合不可用.
  45. postgres=# select any(id) from (values(null),(true),(false)) as t(id);
  46. ERROR: syntax error at or near "any"
  47. LINE 1: select any(id) from (values(null),(true),(false)) as t(id);
  48. ^
  49. postgres=# select some(id) from (values(null),(true),(false)) as t(id);
  50. ERROR: syntax error at or near "some"
  51. LINE 1: select some(id) from (values(null),(true),(false)) as t(id);
  52. ^
  53. bool_or的例子
  54. postgres=# select bool_or(id) from (values(null),(true),(false)) as t(id);
  55. bool_or
  56. ---------
  57. t
  58. (1 row)
  59. 计算非空的表达式个数, count带表达式时, 不计算null
  60. postgres=# select count(id) from (values(null),(1),(2)) as t(id);
  61. count
  62. -------
  63. 2
  64. (1 row)
  65. 计算表达式(含空值)的个数, count(*)计算null, 注意count(*)是一个独立的聚合函数. 请和count(express)区分开来.
  66. postgres=# select count(*) from (values(null),(1),(2)) as t(id);
  67. count
  68. -------
  69. 3
  70. (1 row)
  71. postgres=# select count(*) from (values(null),(null),(1),(2)) as t(id);
  72. count
  73. -------
  74. 4
  75. (1 row)
  76. 聚合后得到json, 不带key的json聚合
  77. postgres=# select json_agg(id) from (values(null),(true),(false)) as t(id);
  78. json_agg
  79. ---------------------
  80. [null, true, false]
  81. (1 row)
  82. 聚合后得到json, 带key的json聚合, 注意key不能为null, 否则报错.
  83. postgres=# select json_object_agg(c1,c2) from (values('a',null),('b',true),('c',false)) as t(c1,c2);
  84. json_object_agg
  85. -----------------------------------------
  86. { "a" : null, "b" : true, "c" : false }
  87. (1 row)
  88. postgres=# select json_object_agg(c1,c2) from (values(null,null),('b',true),('c',false)) as t(c1,c2);
  89. ERROR: 22023: field name must not be null
  90. LOCATION: json_object_agg_transfn, json.c:1959
  91. 计算最大最小值, max, min都不计算null
  92. postgres=# select max(id) from (values(null),(1),(2)) as t(id);
  93. max
  94. -----
  95. 2
  96. (1 row)
  97. postgres=# select min(id) from (values(null),(1),(2)) as t(id);
  98. min
  99. -----
  100. 1
  101. (1 row)
  102. 聚合后得到字符串, 字符串聚合
  103. postgres=# select string_agg(c1,'***') from (values('a',null),('b',true),('c',false)) as t(c1,c2);
  104. string_agg
  105. ------------
  106. a***b***c
  107. (1 row)
  108. postgres=# select string_agg(id,'***') from (values(null),('digoal'),('zhou')) as t(id);
  109. string_agg
  110. ---------------
  111. digoal***zhou
  112. (1 row)
  113. 计算总和, sum不计算null, 当所有行都是null时, 即没有任何行输入, 返回null.
  114. postgres=# select sum(id) from (values(null),(1),(2)) as t(id);
  115. sum
  116. -----
  117. 3
  118. (1 row)
  119. postgres=# select sum(id::int) from (values(null),(null),(null)) as t(id);
  120. sum
  121. -----
  122. (1 row)
  123. 聚合后得到xml
  124. postgres=# select xmlagg(id::xml) from (values(null),('<foo>digoal</foo>'),('<bar/>')) as t(id);
  125. xmlagg
  126. -------------------------
  127. <foo>digoal</foo><bar/>
  128. (1 row)
  129. 某些聚合函数得到的结果可能和行的输入顺序有关, 例如array_agg, json_agg, json_object_agg, string_agg, and xmlagg, 以及某些自定义聚合函数. 如何来实现呢?
  130. 支持聚合函数中使用order by的PostgreSQL版本可以用如下语法 :
  131. postgres=# select string_agg(id,'***' order by id) from (values(null),('digoal'),('zhou')) as t(id);
  132. string_agg
  133. ---------------
  134. digoal***zhou
  135. (1 row)
  136. postgres=# select string_agg(id,'***' order by id desc) from (values(null),('digoal'),('zhou')) as t(id);
  137. string_agg
  138. ---------------
  139. zhou***digoal
  140. (1 row)
  141. 不支持聚合函数中使用order by的PostgreSQL版本, 可以用如下语法 :
  142. SELECT xmlagg(x) FROM (SELECT x FROM test ORDER BY y DESC) AS tab;
  143. postgres=# select string_agg(id,'***') from (select id from (values(null),('digoal'),('zhou')) as t(id) order by id desc) t;
  144. string_agg
  145. ---------------
  146. zhou***digoal
  147. (1 row)
  148. [参考]
  149. 1. http://www.postgresql.org/docs/9.4/static/functions-aggregate.html
  150. 2. http://www.postgresql.org/docs/9.4/static/functions-xml.html
  151. 3. src/backend/utils/adt
  152. 这些函数的代码在src/backend/utils/adt这里可以查询到, 对应各自的类型.
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/470179
推荐阅读
相关标签
  

闽ICP备14008679号