-- 示例-- 示例数据
create table tb(执行日期 datetime,停止日期 datetime,医治 int,名称 varchar(10))
insert tb select '2005-08-20','2005-09-01',' 3','A' --1
union all select '2005-09-01',null ,' 4','B' --2
union all select '2005-06-30','2005-09-06',' 7','C' --3
union all select '2005-09-08',null ,' 9','D' --4
union all select '2005-09-03','2005-09-07','10','E' --5
select * from tb
go--查询处理的存储过程
create proc p_qry
@date_begin datetime,
@date_end datetime
as
set nocount on
select 名称,金额=医治*(
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
go--调用
exec p_qry '2005-9-1','2005-9-30'
go--删除测试
drop table tb
drop proc p_qry/*--结果名称 金额
---------- -----------
A 3
B 120
C 42
D 207
E 50
--*/
create table tb(执行日期 datetime,停止日期 datetime,医治 int,名称 varchar(10))
insert tb select '2005-08-20','2005-09-01',' 3','A' --1
union all select '2005-09-01',null ,' 4','B' --2
union all select '2005-06-30','2005-09-06',' 7','C' --3
union all select '2005-09-08',null ,' 9','D' --4
union all select '2005-09-03','2005-09-07','10','E' --5
select * from tb
go--查询处理的存储过程
create proc p_qry
@date_begin datetime,
@date_end datetime
as
set nocount on
select 名称,金额=医治*(
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
go--调用
exec p_qry '2005-9-1','2005-9-30'
go--删除测试
drop table tb
drop proc p_qry/*--结果名称 金额
---------- -----------
A 3
B 120
C 42
D 207
E 50
--*/
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
怎么办
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 CASE WHEN 停止日期='' THEN @date_end ELSE 停止日期 end>=@date_begin
group by 名称
go
结果是
A
3 B
-154372 C
42 D
-347400 E
50
结果是
A 3
B -154372
C 42
D -347400
E 50
CREATE proc p_qry
@date_begin Char(10),
@date_end Char(10)
as
set nocount on
select 名称,金额=SUM(医治*(
datediff(day,
case when 执行日期<@date_begin then cast(@date_begin as datetime) else cast(执行日期 as datetime) end,
case when 停止日期<@date_end then cast(停止日期 as datetime) else cast(@date_end as datetime) end
)+1))from tb
where 执行日期<=@date_end
and CASE WHEN 停止日期='' THEN @date_end ELSE 停止日期 end>=@date_begingroup by 名称
GO但结果为
A 3
B -154372
C 42
D -347400
E 50
CREATE proc p_qry
@date_begin Char(10),
@date_end Char(10)
as
set nocount on
select 名称,金额=SUM(医治*(
datediff(day,
case when 执行日期<@date_begin then cast(@date_begin as datetime) else cast(执行日期 as datetime) end,
case when (case when 停止日期='' then @date_end else 停止日期 end)<@date_end then cast(停止日期 as datetime) else cast(@date_end as datetime) end
)+1))from tb
where 执行日期<=@date_end
and CASE WHEN 停止日期='' THEN @date_end ELSE 停止日期 end>=@date_begingroup by 名称
GO
则日期是规范的且统一的: yyyy-mm-dd 格式
我也用了但 如果停止日期=NUll isnull(停止日期,@date_end)>=@date_begin 就对
停止日期='' CASE WHEN 停止日期='' THEN @date_end ELSE 停止日期 end>=@date_begin
就错
结果为
A 3
B -154372
C 42
D -347400
E 50