【发布时间】:2021-11-03 22:46:44
【问题描述】:
【问题讨论】:
-
你怎么不关心使用哪个版本的SQL?你不打算用答案吗?!?
标签: sql pivot-table
【问题讨论】:
标签: sql pivot-table
在 Postgres 中,您可以将时间戳聚合到一个数组中:
select sessionid,
class,
starttime,
timestamps[1] as timestamp_1,
timestamps[2] as timestamp_2,
timestamps[3] as timestamp_3,
timestamps[4] as timestamp_4
from (
select sessionid,
class,
starttime,
array_agg(timestamp order by timestamp) as timestamps
from the_table
group by sessionid, class, starttime
) t
SQL 中的列数不能是动态的。解析查询时,数据库引擎必须知道所有列的数量、类型和名称。它在运行时无法更改。
【讨论】: