postgresql - 将列的jsonb行合并到postgres中的json构建对象中
问题描述
共有三列id : integer auto increment , col_jsonb: jsonb , date: timestamp
。我想col_jsonb
基于date
所需的输出
将行值合并到 json 构建对象中
桌子:
+----+----------------+------------+
| id | col_jsonb | date |
+----+----------------+------------+
| 1 | {"Morning":10} | 2020-08-09 |
| 2 | {"Evening":20} | 2020-08-09 |
| 3 | {"Night":30} | 2020-08-09 |
| 4 | {"Morning":20} | 2020-08-10 |
+----+----------------+------------+
预期的o / p:
+----+----------------------------------------------+------------+
| id | col_jsonb | date |
+----+----------------------------------------------+------------+
| 1 | [{"Morning":10},{"Evening":20},{"Night":30}] | 2020-08-09 |
| 2 | {"Morning":20} | 2020-08-10 |
+----+----------------------------------------------+------------+
解决方案
试试这个查询:
select
row_number() over (order by date_) as "id",
jsonb_agg(col_jsonb),
date_ as "Date"
from
example
group by
date_
row_number
如果需要,为行编号添加
推荐阅读
- android - 旋转 YUV420 android.media.Image Android
- angular - 没有获得多个复选框的复选框真值和假值,角度
- flutter - 如何显示列表中的图像
- database - 我无法删除多个表中的所有记录
- javascript - 如何将原始 postgres 查询转换为根据创建的日期列选择当前年份以对查询进行后续处理?
- ios - Xamarin IOS 构建失败 <
> - php - ExtJS sprite 到 PDF,转换单位
- ubuntu - 如果我的配置需要密码,如何将 openvpn 客户端作为守护进程运行
- angular - 角度管理界面
- javascript - 如何使用 window.location.hash 和 reactjs 获取 url 值?