postgre高级postgre 聚合函数数怎么写?想要的效果是这样的

---------------
SELECT id, group_concat(name) from xxx group by id
得出的结果为
id group_concat(name)
---------------------------
PostgreSQL没有现成的group_concat聚集函数,但可以自定义聚集函数,所以可以容易的实现这功能。
自定义聚集函数&group_concat
CREATE AGGREGATE group_concat(anyelement)
sfunc = array_append, -- 每行的操作函数,将本行append到数组里
stype = anyarray, -- 聚集后返回数组类型
initcond = '{}' -- 初始化空数组
参数anyelement匹配任何类型,聚集后返回数组类型anyarray,该函数的功能是将每行的记录附加到数组里。
SELECT id, group_concat(name) from xxx group by id
得出的结果为
id array_accum(name)
---------------------------
1 {'A','B'}
array_accum(name)为数组类型,再用array_to_string函数将数组转换为字符串
SELECT id, array_to_string(group_concat(name),',') from xxx group by id
就可以得到group_concat相同的结果了。
但MySQL的group_concat的功能很强,比如可以排序等,postgresql若要模拟它,只能自己定义一个增强型的函数比如array_to_string_plus,可以对数组进行排序后再concat,这里就不用多述,留给各位动脑筋吧。
select&*&from&t2&where&id&in&(1,2,3,100,1000,0);
select&*&from&t2&where&id&=&ANY&('{1,2,3,100,1000,0}'::integer[]);
//用的这个
select&*&from&t2&where&id&=&ANY&(VALUES&(1),(2),(3),(100),(1000),(0));
select&t2.*&from&t2&join&(VALUES&(1),(2),(3),(100),(1000),(0))&as&t(id)&on&t2.id=t.
2、例子2 &存储过程
CREATE&OR&REPLACE&FUNCTION&存储过程(
&&&&IN&in_no&text,
&&&&IN&in_type&text)
&&RETURNS&TABLE(v_block_id&integer,&v_block_type&integer,&v_center_point&integer,&v_gird_center_point&integer,&v_direction&integer,&v_x&integer,&v_y&integer,&v_lat&numeric,&v_lon&numeric,&v_grid_points&text,&v_points&text)&AS
&&RETURN&QUERY
&&SELECT&b.id,&
&&&&&&&&&b.block_type_id,&
&&&&&&&&&b.center,
&&&&&&&&&b.gridcenter,&
&&&&&&&&&b.dirction,
&&&&&&&&&cg.x_count,
&&&&&&&&&cg.y_count,
&&&&&&&&&cp.x,
&&&&&&&&&cp.y,
&&&&&&&&&array_to_string(platform.array_accum(g.x_count&||&','&||&g.y_count),';'), & //postgresql数据聚集函数array_accum
&&&&&&&&&array_to_string(platform.array_accum(p.x&||&','&||&p.y),';')
&&FROM&表8 b&
&&&&INNER&JOIN&表9 s&
&&&&&&&&&ON&b.station_id&=&s.sid
&&&&INNER&JOIN &表1 bp&
&&&&&&&&&ON&b.id&=&bp.block_id
&&&&LEFT&OUTER&JOIN&表2 &cg&
&&&&&&&&&ON&b.gridcenter&=&cg.id
&&&&LEFT&OUTER&JOIN&表3 &cp&
&&&&&&&&&ON&b.center&=&cp.id
&&&&LEFT&OUTER&JOIN&表4 &g&
&&&&&&&&&ON&bp.girdpoint_id&=&g.id
&&&&LEFT&OUTER&JOIN&表5 p&
&&&&&&&&&ON&bp.point_id&=&p.id
&&WHERE&s.no&=&CAST(in_no&AS&integer)
&&AND&b.block_type_id&=&ANY&(('{'&||&in_type&||&'}')::integer[]) & &//这部分
&&GROUP&BY&b.id,cg.x_count,cg.y_count,cp.x,cp.y;
&&LANGUAGE&plpgsql&VOLATILE;
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:33216次
排名:千里之外
原创:51篇
(1)(2)(2)(8)(2)(1)(2)(1)(4)(7)(2)(5)(6)(4)(5)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'后使用快捷导航没有帐号?
查看: 6067|回复: 4
MongoDB中的聚合函数
金牌会员, 积分 2501, 距离下一级还需 499 积分
论坛徽章:20
除了基本的查询功能外,mongoDB还提供了聚合工具,从简单的计数到使用MapReduce进行复杂数据的分析等。
1.count最简单的聚合工具就是count了,它返回document的数量&&&db.foo.count()
&&&db.foo.insert({ & x &&&:&&1 })
&&&db.foo.count()
也可以传递一个查询条件,计算符合条件的结果个数&&&db.foo.insert({ & x &&&:&&2 })
&&&db.foo.count()
&&&db.foo.count({ & x &&&:&&1 })
2.distinctdistinct命令返回指定的key的所有不同的值。你必须指定一个collection和一个key。&&&db.runCommand({ & distinct &&&:&&& people & ,&&& key &&&:&&& age & })
假设我们的collection里的document是这样子的:{ & name &&&:&&& Ada & ,&&& age &&&:&&20 }
{ & name &&&:&&& Fred & ,&&& age &&&:&&35 }
{ & name &&&:&&& Susan & ,&&& age &&&:&&60 }
{ & name &&&:&&& Andy & ,&&& age &&&:&&35 }
那么返回的结果就是&&&db.runCommand({ & distinct &&&:&&& people & ,&&& key &&&:&&& age & })
{ & values &&&: [ 20 ,&&35 ,&&60 ],&&& ok &&&:&&1 }
3.groupgroup提供了更加复杂的聚合功能,它跟里边的group by很类似,你需要指定一个group by的key,mongoDB按照这个key的值把collection分成不同的组,经过聚合后每个组都产生一个结果document。假设我们有一个站点用来跟踪股票价格,从上午10点到下午4点,每隔几分钟就会有的股票价格存储进数据库,作为报表程序的一部分,我们想找出过去30天的收盘价,使用group就可以很容易做到。股票价格的collection里有成千上万条纪录,格式如下:{ & day &&&:&&&
& ,&&& time &&&:&&& 10/3/:01 GMT-400 & ,&&& price &&&:&&4.23 }
{ & day &&&:&&&
& ,&&& time &&&:&&& 10/4/:39 GMT-400 & ,&&& price &&&:&&4.27 }
{ & day &&&:&&&
& ,&&& time &&&:&&& 10/3/:23 GMT-400 & ,&&& price &&&:&&4.10 }
{ & day &&&:&&&
& ,&&& time &&&:&&& 10/6/:58 GMT-400 & ,&&& price &&&:&&4.30 }
{ & day &&&:&&&
& ,&&& time &&&:&&& 10/4/:50 GMT-400 & ,&&& price &&&:&&4.01 }
我们想要的是每天里边最后成交的那个价钱,结果应该是像下边这样[
{ & time &&&:&&& 10/3/:23 GMT-400 & ,&&& price &&&:&&4.10 },
{ & time &&&:&&& 10/4/:39 GMT-400 & ,&&& price &&&:&&4.27 },
{ & time &&&:&&& 10/6/:58 GMT-400 & ,&&& price &&&:&&4.30 }
那么我们就应该按day分组,找到每组里时间戳的记录,把它放到结果集里&&&db.runCommand({ & group &&&: {
...&&& ns &&&:&&& stocks & ,
...&&& key &&&:&&& day & ,
...&&& initial &&&: { & time &&&:&&0 },
...&&& $reduce &&&:&&function (doc, prev) {
...&&if&&(doc.time&&&&&prev.time) {
...&&prev.price&&=&&doc.
...&&prev.time&&=&&doc.
&ns& : &stocks&
指定对哪个collection运行group命令&key& : &day&
指定按那个key进行分组&initial& : {&time& : 0}
累计器初始值,每个分组第一次调用reduce方法的时候传递给它的值,在一个分组里边,始终使用同一个累计器,对累计器的修改会被保持下来。&$reduce& : function(doc, prev) { ... }
collection里的每个document,都要对之调用reduce方法,传递两个参数给它,第一个是当前的document,第二个是累计器 document,累计器document就是到目前为止分组内的计算结果。(ps:不知道它为啥起个名字叫prev,使用total啊 accumulation之类的不是更容易理解些,我一眼看上去还以为是前一个document。)我们这个例子里,使用reduce方法来比较当前的 document和累计器document的时间,如果当前document的时间更靠后些的话,就是用当前document的值替换累计器 document的值。因为每个组都有各自的累计器,勿需担心日期的不同对累计器的影响。
先前我们说的是取最近30天的价格,我们可以加一个条件,满足条件的才会处理&&&db.runCommand({ & group &&&: {
...&&& ns &&&:&&& stocks & ,
...&&& key &&&:&&& day & ,
...&&& initial &&&: { & time &&&:&&0 },
...&&& $reduce &&&:&&function (doc, prev) {
...&&if&&(doc.time&&&&&prev.time) {
...&&prev.price&&=&&doc.
...&&prev.time&&=&&doc.
...&&& condition &&&: { & day &&&: { & $gt &&&:&&&
如果某些document没有day这个键的话,它们就会被归入到day:null这个组,你可以给condition加个条件&day& : {&$exists& : true}来排除这个组。使用终结器(Finalizer)终结器用于最小化从数据库到用户的数据,我们看一个博客的例子,每篇博客都有几个标签,我们想找出每天最流行的标签是什么。那么我们按照日期进行分组,对每个标签计数:&&&db.posts.group({
...&&& key &&&: { & tags &&&:&&true },
...&&& initial &&&: { & tags &&&: {}},
...&&& $reduce &&&:&&function (doc, prev) {
...&&for&&(i&&in&&doc.tags) {
...&&if&&(doc.tags&&in&&prev.tags) {
...&&prev.tags[doc.tags] ++ ;
...&&}&&else&&{
...&&prev.tags[doc.tags]&&=& &1 ;
返回的结果是下边这个样子[
{ & day &&&:&&&
& ,&&& tags &&&: { &
&&&:&&4 ,&&& winter &&&:&&10 ,&&& sledding &&&:&&2 }},
{ & day &&&:&&&
& ,&&& tags &&&: { & soda &&&:&&5 ,&&& php &&&:&&2 }},
{ & day &&&:&&&
& ,&&& tags &&&: { & python &&&:&&6 ,&&& winter &&&:&&4 ,&&& nosql & :&&15 }}
实际上我们需要的只是值较大的那个标签,并不需要将整个tags返回给客户端,这就是group命令里可选的键&finalize&存在的原因。 finalize指定一个函数,在结果返回给客户端之前,每个分组都会执行一次这个函数。我们使用finalize来去掉不需要的部分。&&&db.runCommand({ & group &&&: {
...&&& ns &&&:&&& posts & ,
...&&& key &&&: { & tags &&&:&&true },
...&&& initial &&&: { & tags &&&: {}},
...&&& $reduce &&&:&&function (doc, prev) {
...&&for&&(i&&in&&doc.tags) {
...&&if&&(doc.tags&&in&&prev.tags) {
...&&prev.tags[doc.tags] ++ ;
...&&}&&else&&{
...&&prev.tags[doc.tags]&&=& &1 ;
...&&& finalize &&&:&&function (prev) {
...&&var&&mostPopular&&=& &0 ;
...&&for&&(i&&in&&prev.tags) {
...&&if&&(prev.tags&&&&&mostPopular) {
...&&prev.tag&&=&&i;
...&&mostPopular&&=&&prev.
...&&delete&&prev.tags
使用函数作为分组key有些情况下,你可能需要更复杂的分组规则,不是一个简单的key,那么你就可以用&$keyf&来定义一个分组函数。&&&db.posts.group({ & ns &&&:&&& posts & ,
...&&& $keyf &&&:&&function (x) {&&return&&x.category.toLowerCase(); },
...&&& initializer &&&: ... })
4.MapReduceMapReduce可是聚合工具里的高级武器,其他工具能做的它能做,其他工具做不了的它也能做。MapReduce是一个在多个服务器间可以并行执行的聚合方法,它将问题分割成多个块,发送给不同的机器,让每个机器解决自己的部分,当所有的机器都完成之后,把所有的结果都合并起来。(ps:这说的貌似 MapReduce最原初的概念,感觉跟我们下边的内容关系不大)MapReduce分两步完成,第一步是映射(Map),将document里的键值投射为一组其他的键值对,第二步是精简(Reduce),将投射出来的键值对按照键合并,每个键最后只有一个值。(ps:这是我的理解,书上写的太拗口)使用MapReduce的代价是速度,group的速度就不咋地,MapReduce更慢,所以一般都是作为后台任务执行,完成之后对其结果collection进行查询。例子1:找出collection里所有的key使用MapReduce解决这个问题确实是杀鸡用牛刀,我们主要是看看MapReduce是如何工作的。MongoDB是无结构的,所以它不会跟踪 document里都有哪些key,我们在这个示例里对collection里的每个key的使用次数进行计数,不包括嵌入的document的key。第一步,映射(Map)使用一个特殊的函数来返回值,这些值后边接下来处理,这个特殊函数就是emit。emit给MapReduce一个key和一个 value,我们这个例子里,我们将document的每个key投射为一个记录其出现次数的数量{count : 1},因为我们要分别记录每个key的出现次数,所以就需要对每个key调用emit函数。&&&map&&=& &function () {
...&&for&&( var&&key&&in& &this ) {
...&&emit(key, {count :&&1 });
现在我们就有了很多的{count : 1},每个都和collection里的一个key关联,相同key的这些{count : 1}构成一个数组被传递给reduce函数,reduce函数有两个参数,第一个是key,就是emit的第一个参数,第二个是数组,包含了被投射在这个 key上的所有{count : 1} 。&&&reduce&&=& &function (key, emits) {
... total&&=& &0 ;
...&&for&&( var&&i&&in&&emits) {
...&&total&&+=&&emits.
...&&return&&{ & count &&&: total};
对来自映射阶段或者前边的reduce阶段的结果,reduce函数必须能够对其重复调用,所以reduce返回的document必须能够重新传递给reduce函数(作为第二个参数)。MapReduce函数的调用结果如下:&&&mr&&=&&db.runCommand({ & mapreduce &&&:&&& foo & ,&&& map &&&: map,&&& reduce &&&: reduce})
& result &&&:&&& tmp.mr.mapreduce__1 & ,
& timeMillis &&&:&&12 ,
& counts &&&: {
& input &&&:&&6
& emit &&&:&&14
& output &&&:&&5
& ok &&&:&&true
&result& : &tmp.mr.mapreduce__1&
存储MapReduce结果的collection的名字,这是个临时的collection,连接关闭后即被删除。我们可以指定一个好听点的名字,并将这个collection永久保存,稍后会讲到。&timeMillis& : 12
操作花费的时间,单位毫秒&counts& : { ... }
&input& : 6& & 传递给map函数的document数量
&emit& : 14& & map函数中调用emit函数的次数
&output& : 5& &结果集collection中document的数量
对结果集collection执行查询就可以看到所有的key和出现次数了&&&db[mr.result].find()
{&&& _id &&&:&&& _id & ,&&& value &&&: {&&& count &&&:&&6&&} }
{&&& _id &&&:&&& a & ,&&& value &&&: {&&& count &&&:&&4&&} }
{&&& _id &&&:&&& b & ,&&& value &&&: {&&& count &&&:&&2&&} }
{&&& _id &&&:&&& x & ,&&& value &&&: {&&& count &&&:&&1&&} }
{&&& _id &&&:&&& y & ,&&& value &&&: {&&& count &&&:&&1&&} }
例子2:对网页分类假设我们有个网站,用户可以提交通向其他页面的链接,用户可以给链接添加一些标签标明这个链接和特定的主题关联,如&政治&,&极客”,&icanhascheezburger&等。(ps:icanhascheezburger是个网站,主题内容是些搞笑的猫咪图片,配些文字说明)我们可以用MapReduce找出那些主题是最近流行的。首先,我们需要一个map函数,根据流行程度和程度将标签投射为一个值。map&&=& &function () {
for&&( var&&i&&in& &this .tags) {
var&&recency&&=& &1 / (new Date() - this.date);
var&&score&&=&&recency&&*& &this .
emit( this .tags, { & urls &&&: [ this .url],&&& score &&&: score});
然后,我们将投射到每个标签的值精简为一个值reduce&&=& &function (key, emits) {
var&&total&&=&&{urls : [], score :&&0 }
for&&( var&&i&&in&&emits) {
emits.urls.forEach( function (url) {
total.urls.push(url);
total.score&&+=&&emits.
这样,结果集里就包含了每个标签的一个url列表和一个标识其流行度的总得分。ps:我们和关系数据库比较一下更容易看到它的关键之处,关键之处就在于emit函数的第一个参数,sql中使用&group by 字段&进行分组,字段的每个不同值就是一个组,而MapReduce中是使用emit为每个字段不同值创建一个key和一个值的数组。简单说,sql的 group by用的是字段名,emit用的是字段的值。明显MapReduce更加灵活强大一些。
mongoDB和MapReduce使用MapReduce命令,除了指定mapreduce,map,reduce这三个必须的键之外,还有很多其他的可选的键。&finalize& : function
终结器函数,接受reduce的输出&keeptemp& : boolean
连接关闭后是否保存临时结果集collection&output& : string
输出collection的名字,使用此选项意味着keeptemp为true&query& : document
查询条件,过滤传递给map函数的document&sort& : document
发送给map函数前对document进行排序,经常是和limit联用&limit& : integer
发送给map函数的document的较大数量&scope& : document
在script代码中可以使用的变量&verbose& : boolean
是否输出更详细的服务器日志
使用scope如果在MapReduce中使用客户端的值,那就必须使用scope选项了。你只需要传递给scope一个 变量名:值 格式的document就可以了,然后这个值在map,reduce以及finalize函数中就可以使用了。这个变量的值在各个函数中是只读的。比如,刚才我们第二个例子中计算页面的性时使用的是1/(new Date() - this.date),如果我们想不使用new Date(),而是把当前日期传递进来的话,就可以定义个叫now的变量&&&db.runCommand({ & mapreduce &&&:&&& webpages & ,&&& map &&&: map,&&& reduce &&&: reduce,
& scope &&&: {now :&&new&&Date()}})
然后在map函数里就可以用1/(now - this.date)了。
金牌会员, 积分 1043, 距离下一级还需 1957 积分
论坛徽章:17
学习了& &mark
中级会员, 积分 420, 距离下一级还需 80 积分
论坛徽章:1
论坛徽章:22postgresql----聚合函数
时间: 00:02:46
&&&& 阅读:528
&&&& 评论:
&&&& 收藏:0
标签:聚合函数是从一组输入中计算出一个结果的函数。
test=# \d tbl_test
Table "public.tbl_test"
| Modifiers
--------+-----------------------+-----------
| character varying(32) |
| character varying(1)
test=# select * from tbl_
id | name | sex
----+------+-----
1 | 张三 | m
2 | 李四 | m
3 | 王五 | f
通用聚合函数
array_agg(expression)
任意非数组类型
参数类型的数组
将入参包括NULL连接成一个数组
select array_agg(id) from tbl_
array_agg(expression)
任意数组类型
入参数据类型
将入参数组连接成更高维度的数组,输入的数组必须是相同的维度,且不允许是空或NULL
select array_agg(array[‘b‘,‘c‘,‘a‘]);
&{{b,c,a}}
avg(expression)
smallint, int, bigint, real, double precision, numeric, or interval
整形返回numeric,浮点型返回double precision,其他和入参类型相同
select avg(id) from tbl_
bit_and(expression)
smallint,&int,&bigint, or&bit
和入参类型相同
所有非NULL输入值的按位与,如果全为NULL则返回NULL
select bit_and(id) from tbl_
bit_or(expression)
smallint,&int,&bigint, or&bit
和入参类型相同
所有非NULL输入值的按位或,如果全为NULL则返回NULL
select bit_or(id) from tbl_
bool_and(expression)
如果输入全是true则返回true,否则为false
select bool_or(id::bool) from tbl_
bool_or(expression)
如果输入至少一个true,则返回true,否则返回false
select bool_or((id-1)::bool) from tbl_
select count(*) from tbl_
count(expression)
输入行中非NULL的行数
select count(id) from tbl_
every(expression)
功能同bool_and
json_agg(expression)
将输入聚合成一个json数组
select json_agg(id) from tbl_
jsonb_agg(expression)
将输入聚合成一个json数组
select jsonb_agg(id) from tbl_
json_object_agg(name,value)
(any, any)
将输入组成一个key/value对的json对象
select json_object_agg(‘a‘,‘one‘);
{ "a" : "one" }
jsonb_object_agg(name,value)
(any, any)
将输入组成一个key/value对的json对象
select jsonb_object_agg(‘a‘,‘one‘);
{"a": "one"}
max(expression)
输入最大值
select max(id) from tbl_
min(expression)
输入最小值
select min(id) from tbl_
string_agg(expression,delimiter)
(text,&text) or (bytea,&bytea)
同参数类型
将输入使用delimiter连接成一个text
select string_agg(name,‘,‘) from tbl_
&张三,李四,王五
sum(expression)
smallint,&int,&bigint,&real,&double precision,&numeric,&interval, or&money
select sum(id) from tbl_
xmlagg(expression)
请参考xml类型及其函数
test=# alter table tbl_test add column id1 int default 1;
ALTER TABLE
test=# select * from tbl_
id | name | sex | id1
----+------+-----+-----
1 | 张三 | m
2 | 李四 | m
3 | 王五 | f
统计聚合函数
corr(Y,&X)
double precision
double precision
select corr(id,id) from tbl_
covar_pop(Y,&X)
double precision
double precision
总体协方差
select covar_pop(id,id) from tbl_
covar_samp(Y,&X)
double precision
double precision
样本协方差
select covar_samp(id,id1) from tbl_
regr_avgx(Y,&X)
double precision
double precision
自变量平均值(sum(X)/N)
select regr_avgx(id,id1) from tbl_
regr_avgy(Y,&X)
double precision
double precision
因变量平均值(sum(Y)/N)
select regr_avgy(id,id1) from tbl_
regr_count(Y,&X)
double precision
两个参数都不为NULL的行数
&select regr_count(id,id1) from tbl_
regr_intercept(Y,&X)
double precision
double precision
根据所有输入点(X,Y)利用最小二乘法计算一个线性方程式。然后返回该直线的Y轴截距
select regr_intercept(id,id) from tbl_
regr_r2(Y,&X)
double precision
double precision
相关系数平方
select regr_r2(id,id) from tbl_
regr_slope(Y,&X)
double precision
double precision
根据所有输入点(X,Y)利用最小二乘法计算一个线性方程式。然后返回该直线的斜率
select regr_slope(id,id) from tbl_
regr_sxx(Y,&X)
double precision
double precision
sum(X^2) - sum(X)^2/N&
select regr_sxx(id,id) from tbl_
regr_sxy(Y,&X)
double precision
double precision
sum(X*Y) - sum(X) * sum(Y)/N&
select regr_sxy(id,id) from tbl_
regr_syy(Y,&X)
double precision
double precision
sum(Y^2) - sum(Y)^2/N&
select regr_syy(id,id) from tbl_
stddev(expression)
smallint, int, bigint, real,
double precision, or numeric
double precision for floating-point arguments,
otherwise numeric
同stddev_samp
stddev_pop(expression)
smallint, int, bigint, real,
double precision, or numeric
double precision for floating-point arguments,
otherwise numeric
总体标准差
select stddev_pop(id) from tbl_
stddev_samp(expression)
smallint, int, bigint, real,
double precision, or numeric
double precision for floating-point arguments,
otherwise numeric
样本标准差
select stddev_samp(id) from tbl_
variance(expression)
smallint, int, bigint, real,
double precision, or numeric
double precision for floating-point arguments,
otherwise numeric
同var_samp
var_pop(expression)
smallint, int, bigint, real,
double precision, or numeric
double precision for floating-point arguments,
otherwise numeric
select var_pop(id) from tbl_
var_samp(expression)
smallint, int, bigint, real,
double precision, or numeric
double precision for floating-point arguments,
otherwise numeric
select var_samp(id) from tbl_
test=# insert into tbl_test values (2,‘ww‘,‘f‘);
INSERT 0 1
test=# select * from tbl_
id | name | sex | id1
----+------+-----+-----
1 | 张三 | m
2 | 李四 | m
3 | 王五 | f
顺序集聚合函数
直接参数类型
聚合参数类型
mode() WITHIN GROUP (ORDER BYsort_expression)
任意可排序类型
同排序类型
返回最频繁的输入值(如果有
多个同样频繁的结果,则返回第一个)
select mode() within group (order by id) from tbl_
percentile_cont(fraction) WITHIN GROUP (ORDER BY&sort_expression)
double precision
double precisionor&interval
同排序类型
continuous percentile: returns a value corresponding to the specified fraction in the ordering, interpolating between adjacent input items if needed
select percentile_cont(0.25) WITHIN GROUP (ORDER BY id) from tbl_
percentile_cont(fractions) WITHIN GROUP (ORDER BY&sort_expression)
double precision[]
double precisionor&interval
array of sort expression‘s type
multiple continuous percentile: returns an array of results matching the shape of the&fractionsparameter, with each non-null element replaced by the value corresponding to that percentile
percentile_disc(fraction) WITHIN GROUP (ORDER BY&sort_expression)
double precision
any sortable type
same as sort expression
discrete percentile: returns the first input value whose position in the ordering equals or exceeds the specified fraction
percentile_disc(fractions) WITHIN GROUP (ORDER BY&sort_expression)
double precision[]
any sortable type
array of sort expression‘s type
multiple discrete percentile: returns an array of results matching the shape of the fractions parameter, with each non-null element replaced by the input value corresponding to that percentile
&&国之画&&&& &&&&chrome插件
版权所有 京ICP备号-2
迷上了代码!

我要回帖

更多关于 sql 聚合函数 的文章

 

随机推荐