- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力对 PostgreSQL 数据库中的 JSONB 字段进行聚合。这可能用一个例子更容易解释,所以如果创建并填充一个名为 analysis
的表,其中包含 2 列(id
和 analysis
),如下所示:-
create table analysis (
id serial primary key,
analysis jsonb
);
insert into analysis
(id, analysis) values
(1, '{"category" : "news", "results" : [1, 2, 3, 4, 5 , 6, 7, 8, 9, 10, 11, 12, 13, 14, null, null]}'),
(2, '{"category" : "news", "results" : [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, null, 26]}'),
(3, '{"category" : "news", "results" : [31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46]}'),
(4, '{"category" : "sport", "results" : [51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66]}'),
(5, '{"category" : "sport", "results" : [71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86]}'),
(6, '{"category" : "weather", "results" : [91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106]}');
如您所见,analysis
JSONB 字段始终包含 2 个属性 category
和 results
。结果属性将始终包含一个大小为 16 的固定长度数组。我使用了各种函数,例如 jsonb_array_elements
但我正在尝试执行以下操作:-
当我想要一个语句返回按类别分组的 3 行(即 news
、sport
和 weather
)和 16 个固定长度包含平均值的数组。更复杂的是,如果数组中有 null
,那么我们应该忽略它们(即我们不是简单地按行数求和和平均)。结果应如下所示:-
category | analysis_average
-----------+--------------------------------------------------------------------------------------------------------------
"news" | [14.33, 15.33, 16.33, 17.33, 18.33, 19.33, 20.33, 21.33, 22.33, 23.33, 24.33, 25.33, 26.33, 27.33, 45, 36]
"sport" | [61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76]
"weather" | [91, 92, 93, 94, 95, 96, 97, 98, 99, 00, 101, 102, 103, 104, 105, 106]
注意:注意第一行最后 2 个数组项中的 45
和 36
,这说明忽略了 nulls
s.
我考虑过创建一个将数组分解为 16 列的 View ,即
create view analysis_view as
select a.*,
(a.analysis->'results'->>0)::int as result0,
(a.analysis->'results'->>1)::int as result1
/* ... etc for all 16 array entries .. */
from analysis a;
这对我来说似乎非常不优雅,并且首先消除了使用数组的优势,但可能会使用这种方法将某些东西组合在一起。
如有任何指点或提示,我们将不胜感激!
性能在这里也很重要,所以性能越高越好!
最佳答案
这适用于任何长度的数组
select category, array_agg(average order by subscript) as average
from (
select
a.analysis->>'category' category,
subscript,
avg(v)::numeric(5,2) as average
from
analysis a,
lateral unnest(
array(select jsonb_array_elements_text(analysis->'results')::int)
) with ordinality s(v,subscript)
group by 1, 2
) s
group by category
;
category | average
----------+----------------------------------------------------------------------------------------------------------
news | {14.33,15.33,16.33,17.33,18.33,19.33,20.33,21.33,22.33,23.33,24.33,25.33,26.33,27.33,45.00,36.00}
sport | {61.00,62.00,63.00,64.00,65.00,66.00,67.00,68.00,69.00,70.00,71.00,72.00,73.00,74.00,75.00,76.00}
weather | {91.00,92.00,93.00,94.00,95.00,96.00,97.00,98.00,99.00,100.00,101.00,102.00,103.00,104.00,105.00,106.00}
关于postgresql - PostgreSQL 中固定大小 JSONB 数组的聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35847467/
我有一个 Postgres 表,mytable 其中一个字段如下: myField JSONB[] NOT NULL 让我们假设上述 json 是这种形式: { "字母":"A", "数字":30}
在 PostgreSQL 的一个 jsonb 字段中保存多个地址是个好主意。我是 nosql 的新手,我想测试 PostgreSQL 来做到这一点。我不想有另一个包含地址的表,我更喜欢将它放在同一个表
我得到了带有 jsonb 的字段 tags: [{"value": "tag1"}] 我需要做这样的事情 update table1 set tags = tags - '{"value": "tag
我使用 postgres9.4,并且存在关系“Patients”具有类型为 jsonb[] 的列“contact”,如何传输类型 jsonb[ ] 到 jsonb? 记录如下。 =>select na
我正在尝试使用以下查询基于 jsonb 字段中的多个 json 属性将 jsonb 字段更新到表中 insert into testtable(data) values('{ "key": "
我有列 options 类型为 jsonb ,格式为 {"names": ["name1", "name2"]} 是用 创建的 UPDATE table1 t1 SET options = (SELE
我在其中一个项目中使用 Postgres 数据库。现在我需要将 JSON 数组存储在数据库中。如下所示: 例如,我有以下 JSON 结构: [ { "Id": 1, "Name":
我正在尝试从 postgre 表中读取一个 jsonb 字段。我尝试通过以下方式使用 Jsonb 库将其作为 Jsonb 类型读取: @Entity @Table(name = "test") dat
tl;dr -- 有什么方法可以从 postgres 中的 jsonb 对象获取值作为 jsonb_array 吗? 尝试在 postgres 中使用递归 cte 来展平任意深度的树结构,如下所示:
给定类似于以下的现有数据结构: CREATE TEMP TABLE sample (id int, metadata_array jsonb, text_id_one jsonb, text_id_t
psql --version psql (PostgreSQL) 9.4.1 rails -v Rails 4.2.0 我通过这样的迁移添加了一个 jsonb 列 class AddPreferenc
假设我有一个模型 Neighborhood,它有一个 jsonb[] 字段 families,它是一个包含 json 对象的数组,具有任何类型的键值对,如下所示 [{"name":"Smiths",
我在这里显示了以下数据集 http://sqlfiddle.com/#!17/f9280/1 我想查询数据的方式是每个键和类别的平均排名 例如, 键 1,类别 10,avg_rank:95 键 1,类
我在 springboot(2.1)+postgres(10.5)+hibernate(5.3.7) 中使用 jsonb。 以下是文件中的更改: 在 pom.xml 中 .... com.vladm
考虑像下面这样的模型和一个 bars 的 json 数组: const myModel = { id: 1, bars: [ { aproperty: 10 },
friend 们好,我需要帮助来解决以下问题, 我在我的 postgres 数据库表中有一组记录,其中表有 JSONB 类型字段。 JSONB 类型列包含以下JSON, 记录#1 :- { "ke
我有一个表,其中 JSONB 列存储 JSONB 数组/字符串(下例中的 value_r 列)。仅对 JSONB 列中的 JSONB 数组的内容进行排序(还存储字符串)的最简单(且有效)的方法是什么?
在 PostgreSQL 9.5 表中,我有一个 integer 列 social。 当我尝试在存储过程中更新它时,在jsonb 类型的 in_users 变量中给定以下 JSON 数据(一个包含 2
我有一个专栏amount_splits我需要按照我指定的键顺序保存我的 JSON。 如何防止 Rails/Postgres jsonb当我将它保存到数据库时自动排序我的 JSON 键? (用于创建或更
我想使用 UNION ALL 运算符将 2 个表合并为一个表。第一个表有几个字段。第二个表将几个字段分组到 JSONB 字段上。 为了简化问题,我使用这个简单的 SQL 请求(不依赖于表)重现了错误:
我是一名优秀的程序员,十分优秀!