PostgreSQL 9.5新特性 width_bucket求位置插值,可展示柱状图

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
简介:
PostgreSQL 9.5 新增的一个函数width_bucket , 用来计算一个值在一个bucket范围内的位置信息,如果这个值超出了给定的范围,返回0或者总bucket+1。bucket除了用数字来表示,还可以直接用数组来表示。
width_bucket(operand dp,b1 dp, b2 dp, count int) int return the bucket number to which operand would be assigned in a histogram having count equal-width buckets spanning the range b1 to b2; returns 0 or count+1 for an input outside the range width_bucket(5.35, 0.024, 10.06, 5) 3
width_bucket(operandnumeric, b1 numeric, b2numeric, count int) int return the bucket number to which operand would be assigned in a histogram having count equal-width buckets spanning the range b1 to b2; returns 0 or count+1 for an input outside the range width_bucket(5.35, 0.024, 10.06, 5) 3
width_bucket(operandanyelement, thresholdsanyarray) int return the bucket number to which operand would be assigned given an array listing the lower bounds of the buckets; returns 0 for an input less than the first lower bound; the thresholds arraymust be sorted, smallest first, or unexpected results will be obtained width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[]) 2
例子:
0.0, 5.0是边界,其中包含0.0,但是不包含5.0, 最后一个参数表示一共分为5个bucket。
超出边界:
 
  

postgres=# select width_bucket(-1, 0.0, 5.0, 5);
 width_bucket 
--------------
            0
(1 row)
postgres=# select width_bucket(5.0, 0.0, 5.0, 5);
 width_bucket 
--------------
            6
(1 row)
postgres=# select width_bucket(-0.0000001, 0.0, 5.0, 5);
 width_bucket 
--------------
            0
(1 row)
postgres=# select width_bucket(5.1, 0.0, 5.0, 5);
 width_bucket 
--------------
            6
(1 row)


在边界内:
 
  

postgres=# select width_bucket(0, 0.0, 5.0, 5);
 width_bucket 
--------------
            1
(1 row)
postgres=# select width_bucket(1, 0.0, 5.0, 5);
 width_bucket 
--------------
            2
(1 row)
postgres=# select width_bucket(1.9, 0.0, 5.0, 5);
 width_bucket 
--------------
            2
(1 row)
postgres=# select width_bucket(1.9999999, 0.0, 5.0, 5);
 width_bucket 
--------------
            2
(1 row)
postgres=# select width_bucket(2, 0.0, 5.0, 5);
 width_bucket 
--------------
            3
(1 row)
postgres=# select width_bucket(4.9999, 0.0, 5.0, 5);
 width_bucket 
--------------
            5
(1 row)


直接使用数组代表边界:
注意参数类型必须一致。
 
  

postgres=# select width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamptz[]);
 width_bucket 
--------------
            2
(1 row)
postgres=# select width_bucket(now(), array['yesterday', 'today', 'tomorrow']::timestamp[]);
ERROR:  function width_bucket(timestamp with time zone, timestamp without time zone[]) does not exist
LINE 1: select width_bucket(now(), array['yesterday', 'today', 'tomo...
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
postgres=# select width_bucket(1,'{0,1,100,200,300}'::int[]);
 width_bucket 
--------------
            2
(1 row)

边界表示如下,所以1落在第二个bucket
[0,1)
[1,100)
[100,200)
[200,300)


[参考]
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
SQL 关系型数据库 OLAP
|
10月前
|
存储 关系型数据库 数据库
探索PostgreSQL 14新特性--SEARCH和CYCLE
探索PostgreSQL 14新特性--SEARCH和CYCLE
54 0
|
10月前
|
存储 缓存 关系型数据库
PostgreSQL 14新特性--减少索引膨胀
PostgreSQL 14新特性--减少索引膨胀
402 0
|
SQL 存储 缓存
从研发角度深入了解RDS AliSQL内核2020新特性
内容简要: 一、关于内核 二、内核特性详解 一、 关于内核 (一)回归内核
从研发角度深入了解RDS AliSQL内核2020新特性
|
SQL 存储 缓存
从研发角度深入了解RDS AliSQL内核2020新特性 ——楼方鑫(黄忠)
从研发角度深入了解RDS AliSQL内核2020新特性 ——楼方鑫(黄忠)
从研发角度深入了解RDS AliSQL内核2020新特性    ——楼方鑫(黄忠)
|
SQL AliSQL Cloud Native
RDS发布会解读 | AliSQL内核新特性
AliSQL在2020年做了不少事情,有必要总结分享一下,以便让大家更好地知道有哪些特性,可以在哪些业务场景中使用到,也是为了在2021年更好地向前发展。
439 0
RDS发布会解读 | AliSQL内核新特性
|
SQL AliSQL Cloud Native
RDS发布会解读| AliSQL内核新特性
AliSQL在2020年做了不少事情,有必要总结分享一下,以便让大家更好地知道有哪些特性,可以在哪些业务场景中使用到,也是为了在2021年更好的向前发展。在年初时计划的一些企业级功能基本上都实现了,并且在过程中特别强调了功能的场景通用性,不再是从某个行业某个特定业务或应用场景设计(比如电商秒杀),而是从云上众多用户的不同场景出发,并且不需要用户应用或SQL改造配合(直接一个开关就可以开启的),还要求在RDS 56/57/80三个主流版本上都有同样的体验,从云场景而生并为云场景服务的技术,都是云原生技术。这一目标角度的调整的确是给自己加了不少难度,但研发让所有云上用户都能轻松受益享受技术红利的新
2465 0
RDS发布会解读| AliSQL内核新特性
|
SQL 存储 Oracle
PostgreSQL Oracle 兼容性 - Oracle 19c 新特性在PostgreSQL中的使用
PostgreSQL Oracle 兼容性 - Oracle 19c 新特性在PostgreSQL中的使用
2814 0
|
SQL 存储 Oracle
PostgreSQL Oracle 兼容性 - Oracle 19c 新特性在PostgreSQL中的使用
标签 PostgreSQL , Oracle 兼容性 , Oracle 19c 背景 《PostgreSQL 覆盖 Oracle 18c 重大新特性》 Oracle 19c 新特性摘自盖老师《Oracle 19c 新特性及官方文档抢鲜下载》文章,其中有一些特性在PostgreSQL中很早以前已经支持。本文旨在介绍PG如何使用这些特性。 1.Data Guard 备库DML自动重定向
679 0
|
数据格式 JSON 索引
AnalyticDB for PostgreSQL 6.0新特性 JSONB数据类型
ADB PG 6.0 JSONB数据类型特性 JSON Types JSON数据类型顾名思义是用来存储JSON数据的,这种数据也可以用text类型来存储,但是JSON数据类型会对数据做JSON规则校验,同时提供一些列的特定的JSON化的函数,让用户可以对这些数据作出一些特殊的操作。
2802 0

相关产品

  • 云原生数据库 PolarDB