[英]pivot table with multiple value columns in postgres
I need a query which will help me to get data as described below我需要一个查询来帮助我获取如下所述的数据
I have a table as followed我有一张桌子如下
ID ID | date日期 | STATUS地位 | TIME09_10 TIME09_10 | TIME10_11 TIME10_11 | TIME11_12 TIME11_12 | TIME12_13 TIME12_13 |
---|---|---|---|---|---|---|
1 1 | 2021-09-01 2021-09-01 | RUN跑 | 30 30 | 60 60 | 45 45 | 0 0 |
2 2 | 2021-09-01 2021-09-01 | WALK走 | 15 15 | 0 0 | o ○ | 30 30 |
3 3 | 2021-09-01 2021-09-01 | STOP停止 | 15 15 | 0 0 | 15 15 | 30 30 |
I want this data to be in below format.我希望这些数据采用以下格式。 From the above table , value columns name has to be replaced with hour and join with date column.从上表中,值列名称必须替换为小时并与日期列连接。 I would really appreciate someone's help.我真的很感激有人的帮助。 I am using postgres sql database.我正在使用 postgres sql 数据库。
date & time约会时间 | run跑 | walk走 | stop停止 |
---|---|---|---|
2021-09-01 09:00 2021-09-01 09:00 | 30 30 | 15 15 | 15 15 |
2021-09-01 10:00 2021-09-01 10:00 | 60 60 | 0 0 | 0 0 |
2021-09-01 11:00 2021-09-01 11:00 | 45 45 | 0 0 | 15 15 |
2021-09-01 12:00 2021-09-01 12:00 | 0 0 | 30 30 | 30 30 |
You can use the crosstab(source_sql text, category_sql text)
function.您可以使用crosstab(source_sql text, category_sql text)
函数。 You need to install the tablefunc
extension:您需要安装tablefunc
扩展:
create extension if not exists tablefunc;
Read about the extension in the documentation.阅读文档中的扩展。
The function expects data in the three-column format (row_name, category, value)
.该函数需要三列格式(row_name, category, value)
。 In this case they are date+time
, status
and duration
.在这种情况下,它们是date+time
、 status
和duration
。
select
date+ '8 hour'::interval+ '1 hour'::interval* i as hour,
status,
(array[time09_10, time10_11, time11_12, time12_13])[i] as duration
from my_table
cross join generate_series(1, 4) i
hour | status | duration
---------------------+--------+----------
2021-09-01 09:00:00 | RUN | 30
2021-09-01 09:00:00 | WALK | 15
2021-09-01 09:00:00 | STOP | 15
2021-09-01 10:00:00 | RUN | 60
2021-09-01 10:00:00 | WALK | 0
2021-09-01 10:00:00 | STOP | 0
2021-09-01 11:00:00 | RUN | 45
2021-09-01 11:00:00 | WALK | 0
2021-09-01 11:00:00 | STOP | 15
2021-09-01 12:00:00 | RUN | 0
2021-09-01 12:00:00 | WALK | 30
2021-09-01 12:00:00 | STOP | 30
(12 rows)
Pass the query as the first argument to the function:将查询作为第一个参数传递给函数:
select *
from crosstab(
$source$
select
date+ '8 hour'::interval+ '1 hour'::interval* i as hour,
status,
(array[time09_10, time10_11, time11_12, time12_13])[i] as duration
from my_table
cross join generate_series(1, 4) i
$source$,
$category$
values('RUN'), ('STOP'), ('WALK')
$category$
) as (hour timestamp, run int, stop int, walk int)
hour | run | stop | walk
---------------------+-----+------+------
2021-09-01 09:00:00 | 30 | 15 | 15
2021-09-01 10:00:00 | 60 | 0 | 0
2021-09-01 11:00:00 | 45 | 15 | 0
2021-09-01 12:00:00 | 0 | 30 | 30
(4 rows)
There is a nice alternative if you do not want to use the extension.如果您不想使用扩展程序,还有一个不错的选择。 Convert the first query results to the expected output with the jsonb
function:使用jsonb
函数将第一个查询结果转换为预期输出:
select
hour,
(activities->>'RUN')::int as run,
(activities->>'STOP')::int as stop,
(activities->>'WALK')::int as walk
from (
select hour, jsonb_object_agg(status, duration) as activities
from (
select
date+ '8 hour'::interval+ '1 hour'::interval* i as hour,
status,
(array[time09_10, time10_11, time11_12, time12_13])[i] as duration
from my_table
cross join generate_series(1, 4) i
) s
group by hour
) s
order by hour
Test the jsonb
solution in Db<>fiddle.在Db<>fiddle 中测试jsonb
解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.