当前位置: 代码迷 >> Sql Server >> 交叉取时间解决方法
  详细解决方案

交叉取时间解决方法

热度:58   发布时间:2016-04-27 13:12:00.0
交叉取时间
字段
SQL code
startDate  endDate2012-1-1   2012-1-1...         ...2012-1-15  2012-1-16


表中数据是出差的记录
现在要把多条记录合成一条记录,开始日期取第一条的开始日期,结束日期取最后一条的结束日期

请大家帮忙

------解决方案--------------------
只取两条记录么。
------解决方案--------------------
SQL code
create table tb( startdate datetime, enddate datetime)insert into tbselect '2012-1-1','2012-1-1' union allselect '2012-1-4','2012-1-6' union allselect '2012-1-15','2012-1-16' select MIN (startdate),MAX (enddate) from tb
------解决方案--------------------
SQL code
create table mpt(startDate date, endDate date)insert into mptselect '2012-1-1', '2012-1-1' union allselect '2012-1-15', '2012-1-16'with t as(select row_number() over(order by (select 0)) rn,startDate,endDate from mpt)select (select startDate from t where rn=1) startDate,(select top 1 endDate from t order by rn desc) endDatestartDate  endDate---------- ----------2012-01-01 2012-01-16(1 row(s) affected)
  相关解决方案