我在表格中有jsonb
字段,其值如下:
{
"1":[{"start":64800,"finish":68400},{"start":61200,"finish":64800},{"start":75600,"finish":79200},{"start":79200,"finish":82800}],
"2":[{"start":68400,"finish":72000},{"start":72000,"finish":75600},{"start":75600,"finish":79200},{"start":79200,"finish":82800}],
"3":[{"start":46800,"finish":50400},{"start":50400,"finish":54000}],
"4":[{"start":50400,"finish":54000}],
"5":[{"start":79200,"finish":82800},{"start":82800,"finish":0},{"start":0,"finish":3600}],
"6":[{"start":68400,"finish":72000},{"start":72000,"finish":75600},{"start":79200,"finish":82800}]
}
0 ... 6 - 星期几,这是一系列的工作时间。
所以我需要按周计算工作时间。例如,一行有
"5":[{"start":79200,"finish":82800},{"start":82800,"finish":0},{"start":0,"finish":3600}]
另一行有
"5":[{"start":75600,"finish":79200}]
我希望得到
"5":[{"start":75600,"finish":79200},{"start":79200,"finish":82800},{"start":82800,"finish":0},{"start":0,"finish":3600}]
答案 0 :(得分:0)
如果您需要手动运行此报告,请执行以下操作:不规则地,你可以自己做within PostgreSQL。但是如果性能对您很重要,那么您应该规范化您的模式(例如,如果您的jsonb
列的结构很简单,那么根本就没有必要使用JSON:您没有任何非结构化数据)
SELECT jsonb_object_agg(dow, working_times_agg)
FROM (SELECT dow, jsonb_agg(working_time) working_times_agg
FROM table t,
jsonb_each(t.jsonb_column) o(dow, working_times),
jsonb_array_elements(working_times) working_time
GROUP BY dow) working_times_agg_by_dow