栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/634607.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号