日期:2014-05-18 浏览次数:20847 次
create table tb (策略ID int, 按小时数 int, 有效时间从 varchar(20), 至 varchar(20), 第N天 int, 时间 varchar(20), 价格 int)
insert tb select 100001,3,'00:00:00','00:00:00', 0, NULL, 298
insert tb select 100002,6,'00:00:00','00:00:00', 0, NULL, 560
insert tb select 100003,0,'12:00:00','22:00:00', 2, '12:00',1580
insert tb select 100004,3,'03:00:00','06:00:00', 0, NULL, 0 --本行似乎有问题,不要钱么?
insert tb select 100005,0,'21:00:00','12:00:00', 2, '12:00',980
--建一个函数,返回最优价格
if object_id('getprice','FN') is not null
drop function getprice
go
--参数分别为入住时间,结算时间,1小时单价
create function getprice(@sdate datetime, @jsdate datetime, @hourprice int)
returns int
as
begin
declare @minprice int
declare @hours int,@days int
set @hours = ceiling(datediff(mi,@sdate,@jsdate)*1.0/60)
set @days = datediff(d,@sdate,@jsdate)
--取出策略中的最小价格
select @minprice = min(case 按小时数 when 0 then
case when @days = 0 then 1 when convert(varchar(8),@jsdate,108) > convert(varchar(8),convert(datetime,'12:00',108),108) then @days+1 else @days end * 价格
else ceiling(@hours*1.0/按小时数)*价格 end)
from tb
where (有效时间从 = '00:00:00' and 至 = '00:00:00') or (convert(varchar(8),getdate(),108) between 有效时间从 and 至)
--未考虑策略中无价格(即@minprice is null)的情况,请自行补充
return case when @minprice < @hours * @hourprice then @minprice else @hours * @hourprice end
end
select dbo.getprice('2010-12-01 11:08:32','2010-12-01 12:04:13',100)
-----------
100
select dbo.getprice('2010-12-01 11:08:32','2010-12-01 14:04:13',100)
-----------
298
------解决方案--------------------
100001 3 00:00:00 00:00:00 0 298
100002 6 00:00:00 00:00:00 0 560
100003 0 12:00:00 22:00:00 2 12:00 1580
100004 3 03:00:00 06:00:00 0 0
100005 0 21:00:00 12:00:00 2 12:00 980
能解释下这段是什么意思吗,为什么有的时间段是不要钱的,还有像100005 0 21:00:00 12:00:00 2 12:00 980这第二个零是什么意思
------解决方案--------------------