查询任一时间段的数据
如:2005-09-01 到 2005-09-30
但要根据执行日期,和停止日期来查
执行日期 停止日期 医治 名称
2005-08-20 2005-09-01 3 A --1
2005-09-01 4 B --2
2005-06-30 2005-09-06 7 C --3
2005-09-08 9 D --4
2005-09-03 2005-09-07 10 E --5
停止日期为空代表还没停止,还在执行
现要查2005-09-01 到 2005-09-30 间的数据
第1条有一天那是1*3(天数*医治),第2条没停止日期那就是30天那是30*4
第3天是6天那是6*7,第4天是22天那是22*9 等等...
要得到
A 3
B 120
C 48
D 198
.
.
.
帮帮我,很急....
--改一下,应该加汇总
--查询处理的存储过程
create proc p_qry
@date_begin datetime,
@date_end datetime
as
set nocount on
select 名称,金额=SUM(医治*(
datediff(day,
case when 执行日期<@date_begin then @date_begin else 执行日期 end,
case when 停止日期<@date_end then 停止日期 else @date_end end
)+1))
from tb
where 执行日期<=@date_end
and isnull(停止日期,@date_end)>=@date_begin
group by 名称
go