postgresql - 如何在postgres中旋转多个列
问题描述
我在 Postgres 中有一张表,我想在 2 列上从宽转长。
数据源太大而无法使用 Python,因为这需要加载到内存中。Postgres 中是否有能够做到这一点的函数?
下面是表格的样子...
date a1_on_time a1_days b2_on_time b2_days
15-Apr-19 TRUE 1 TRUE 1
26-Apr-19 TRUE 2 FALSE 6
输出应如下所示:
date metric on_time days
15-Apr-19 a1 TRUE 1
26-Apr-19 a1 TRUE 2
15-Apr-19 b2 TRUE 1
26-Apr-19 b2 FALSE 6
任何想法将不胜感激。
解决方案
使用联合查询:
select date, 'a1' as metric, a1_on_time as on_time, a1_days as days from your_table
union all
select date, 'b2', b2_on_time, b2_days from your_table
order by metric, date;