如果只有三个日期,则
pivot/ conditional合计应该可以:
select id, max(case when seqnum = 1 then dstart end) as start_1, max(case when seqnum = 1 then dend end) as end_1, max(case when seqnum = 2 then dstart end) as start_2, max(case when seqnum = 2 then dend end) as end_2, max(case when seqnum = 3 then dstart end) as start_3, max(case when seqnum = 3 then dend end) as end_3from (select t.*, row_number() over (partition by id order by dstart) as seqnum from t ) tgroup by id;
注意:您必须指定输出中的列数。如果您不知道有多少个,可以:
- 生成动态SQL语句以预先进行计数。
- 手动计算自己并添加适当的列。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)