我目前拥有以下表单的一些数据。Postgres/AWS Redshift:将数据透视数据转换为长格式
userid | event_time | event_duration (secs) | first_activity | last_activity
A 2017-01-01 02:20:34 16 E1 E2
A 2017-03-01 11:23:43 12 E2 E6
B 2017-01-01 08:24:32 53 E1 E4
C 2017-01-01 23:19:21 43 E3 E11
我想进入它的形式:
userid | event_time | activity
A 2017-01-01 02:20:34 E1
A 2017-01-01 02:20:50 E2
A 2017-03-01 11:23:43 E2
A 2017-03-01 11:23:55 E6
B 2017-01-01 08:24:32 E1
B 2017-01-01 08:25:25 E4
C 2017-01-01 23:19:21 E3
C 2017-01-01 23:20:04 E11
我可以做到这一点很容易:
SELECT userid, event_time, first_activity
FROM table
UNION
SELECT userid, event_time + event_duration * interval '1 seconds', last_activity
FROM table
不过,我想办法,以避免重复查询两次使用UNION
s。有两种方法可以创建我需要的表单而不需要两个查询吗?
如果您使用Redshift,请勿使用Postgres标记问题。 [来自Redshift手册](http://docs.aws.amazon.com/redshift/latest/dg/c_unsupported-postgresql-features.html):“*不要认为Amazon Redshift和PostgreSQL具有的元素的语义共同点是相同的*“ –