34,576
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据:@tb
declare @tb table([项目名称] varchar(1),[开始日期] datetime,[数值] int,[结束日期] datetime)
/*insert @tb
select 'a','20050617',100,'20060516' union all
select 'b','20060103',300,'20060817' union all
select 'c','20050923',400,'20070318' union all
select 'd','20070711',200,'20080102'*/
insert into @tb values('a', '20060111' , 21850000 , '20060711')
insert into @tb values('b', '20060307' , 13300000 , '20060906')
insert into @tb values('c', '20060831' , 11400000 , '20070228')
insert into @tb values('d', '20051017' , 8500000 , '20060117')
--select * from @tb
declare @StartDate datetime, @EndDate datetime
select @StartDate = min([开始日期]), @EndDate=max([结束日期]) from @tb
declare @Date table(Date datetime)
set nocount on
while @StartDate<@EndDate
begin
insert @Date select @StartDate
set @StartDate=@StartDate+1
end
set nocount off
select max(Total) from
(
select Date, Total=sum(case when Date between [开始日期] and [结束日期] then [数值] else 0 end)
from @tb, @Date
group by Date
) T
/*
-----------
800
(1 行受影响)
*/
/*
-----------
35150000
(1 行受影响)
*/
--> 测试数据:@tb
declare @tb table([项目名称] varchar(1),[开始日期] datetime,[数值] int,[结束日期] datetime)
insert @tb
/*
select 'a','20050617',100,'20060516' union all
select 'b','20060103',300,'20060817' union all
select 'c','20050923',400,'20070318' union all
select 'd','20070711',200,'20080102'
*/
select
'a', '20060111', 21850000, '20060711'
union all select
'b', '20060307', 13300000, '20060906'
union all select
'c', '20060831', 11400000, '20070228'
union all select
'd', '20051017', 8500000 , '20060117'
--时间段
declare @time table (
日期 datetime,
序号 int IDENTITY(1,1)
)
insert @time
select 开始日期 as 日期 from @tb
union
select 结束日期 as 日期 from @tb
order by 日期
select top 1 sum(a.数值) as 数值 from @tb a ,@Time t1,@Time t2
where t2.序号=t1.序号+1
and (a.开始日期 < t2.日期
and a.结束日期 > t1.日期
)
group by t1.日期,t2.日期
order by sum(a.数值) desc
--结果
数值
-----------
35150000
(所影响的行数为 1 行)
create table tb(项目名称 varchar(10) , 开始日期 datetime, 数值 bigint, 结束日期 datetime)
insert into tb values('a', '20060111' , 21850000 , '20060711')
insert into tb values('b', '20060307' , 13300000 , '20060906')
insert into tb values('c', '20060831' , 11400000 , '20070228')
insert into tb values('d', '20051017' , 8500000 , '20060117')
go
--临时表
select top 8000 identity(int,0,1) as id into tmp from syscolumns a,syscolumns b
select t1.数值 + t2.数值 数值 from tb t1,
(
select sum(distinct n.数值) 数值 from tb m,
(select 项目名称 , dateadd(day , tmp.id , 开始日期) 日期 , 数值 from tb , tmp where dateadd(day , tmp.id , 开始日期) <= 结束日期) n,
(select top 1 项目名称 from tb order by 项目名称) o
where m.项目名称 = o.项目名称 and n.项目名称 <> o.项目名称 and n.日期 between m.开始日期 and m.结束日期
) t2,
(select top 1 项目名称 from tb order by 项目名称) t3
where t1.项目名称 = t3.项目名称
drop table tb , tmp
/*
数值
--------------------
43650000
(所影响的行数为 1 行)
*/
--> 测试数据:@tb
declare @tb table([项目名称] varchar(1),[开始日期] datetime,[数值] int,[结束日期] datetime)
insert @tb
select 'a','20050617',100,'20060516' union all
select 'b','20060103',300,'20060817' union all
select 'c','20050923',400,'20070318' union all
select 'd','20070711',200,'20080102'
--时间段
declare @time table (
日期 datetime,
序号 int IDENTITY(1,1)
)
insert @time
select 开始日期 as 日期 from @tb
union
select 结束日期 as 日期 from @tb
order by 日期
select top 1 sum(a.数值) as 数值 from @tb a ,@Time t1,@Time t2
where t2.序号=t1.序号+1
and (a.开始日期 <= t2.日期
and a.结束日期 >= t1.日期
)
group by t1.日期,t2.日期
order by sum(a.数值) desc