首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求大家帮忙 去掉重复的天数,该如何解决

2012-01-15 
求大家帮忙去掉重复的天数我的提个数据表TBTest有两个字段BeginDateEndDate现有如下3条记录2007-1-12007-1

求大家帮忙 去掉重复的天数
我的提个数据表TBTest
有两个字段BeginDate EndDate

现有如下3条记录
2007-1-1 2007-1-10
2007-1-3 2007-1-20
2007-2-1 2007-2-10

我想减去 重复的天数 得到
28天

感谢大家帮助

[解决办法]
怎么个去法? 是从BeginDate里去, 还是从EndDate里去?
[解决办法]
怎么个减法?
楼主描述清楚一点`
[解决办法]
select datediff(d,min(BeginDate),max( EndDate )) from t
[解决办法]
你要写一个函数/过程
这个函数将所有日期列出到一个临时表,然后distinct查询日期

假设
循环日期

while @nowdate< @enddate
loop 
 insert into #t
@nowdate
@nowdate=@nowdate+1
end loop
[解决办法]

SQL code
declare @t table(id int identity(1,1),begindate datetime,enddate datetime)insert @t select '2007-1-1','2007-1-10'union all select '2007-1-3','2007-1-20'union all select '2007-2-1 ','2007-2-10'select     sum(datediff(day,a.begindate,case when a.enddate > b.begindate then b.begindate else a.enddate end)) as diffdayfrom @t aouter apply(    select begindate    from @t    where id = a.id + 1)b 

热点排行