SQL Server将多行合并为具有多列的一行

SQL Server将多行合并为具有多列的一行,第1张

SQL Server将多行合并为具有多列的一行

如果只有三个日期,则

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语句以预先进行计数。
  • 手动计算自己并添加适当的列。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5640478.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存