pivot table 在 postgres 中有多个值列
pivot table with multiple value columns in postgres
我需要一个查询来帮助我获取如下所述的数据
我有一个table如下
ID
date
STATUS
TIME09_10
TIME10_11
TIME11_12
TIME12_13
1
2021-09-01
RUN
30
60
45
0
2
2021-09-01
WALK
15
0
o
30
3
2021-09-01
STOP
15
0
15
30
我希望此数据采用以下格式。从上面的 table 中,值列名称必须替换为小时并与日期列连接。我真的很感激有人的帮助。
我正在使用 postgres sql 数据库。
date & time
run
walk
stop
2021-09-01 09:00
30
15
15
2021-09-01 10:00
60
0
0
2021-09-01 11:00
45
0
15
2021-09-01 12:00
0
30
30
您可以使用crosstab(source_sql text, category_sql text)
功能。您需要安装 tablefunc
扩展:
create extension if not exists tablefunc;
了解扩展程序 in the documentation.
该函数需要三列格式的数据 (row_name, category, value)
。在这种情况下,它们是 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)
将查询作为函数的第一个参数传递:
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)
如果您不想使用该扩展程序,还有一个不错的选择。使用 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
中测试jsonb
解决方案
我需要一个查询来帮助我获取如下所述的数据
我有一个table如下
ID | date | STATUS | TIME09_10 | TIME10_11 | TIME11_12 | TIME12_13 |
---|---|---|---|---|---|---|
1 | 2021-09-01 | RUN | 30 | 60 | 45 | 0 |
2 | 2021-09-01 | WALK | 15 | 0 | o | 30 |
3 | 2021-09-01 | STOP | 15 | 0 | 15 | 30 |
我希望此数据采用以下格式。从上面的 table 中,值列名称必须替换为小时并与日期列连接。我真的很感激有人的帮助。 我正在使用 postgres sql 数据库。
date & time | run | walk | stop |
---|---|---|---|
2021-09-01 09:00 | 30 | 15 | 15 |
2021-09-01 10:00 | 60 | 0 | 0 |
2021-09-01 11:00 | 45 | 0 | 15 |
2021-09-01 12:00 | 0 | 30 | 30 |
您可以使用crosstab(source_sql text, category_sql text)
功能。您需要安装 tablefunc
扩展:
create extension if not exists tablefunc;
了解扩展程序 in the documentation.
该函数需要三列格式的数据 (row_name, category, value)
。在这种情况下,它们是 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)
将查询作为函数的第一个参数传递:
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)
如果您不想使用该扩展程序,还有一个不错的选择。使用 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
中测试jsonb
解决方案