日期:2014-05-18  浏览次数:20701 次

悬赏200RMB红包+100分请教一个SQL语句,求高手了,多谢
客房号 入住时间 1小时单价 结算时间
201 2010-12-01 11:08:32 100 传入值
202 2010-12-01 06:23:44 100 传入值


策略ID 按小时数 有效时间从 至 第N天 时间 价格
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

注:如果按小时数值为0,则按天,按以上1000002数据,则表示入住的第二天,12:00则表示,第二天的12:00之前


计算出在任意的时间,所有在住房间房价的最优价格,假设表1的201来说:
比如传入结算时间为:2010-12-01 12:04:13 则是1小时不到,取1小时价即100元
如传入:2010-12-01 14:04:13 则是超过2小时不足3小时,按单小时来算的话因为在满足2小时时就会按200,超过2小时后,超出部分按最低的1小时即200+100元=300,这时按这个算法已经超过按3小时策略价格的298,则取298元

如此类推,总归是要计算出201房间在任何时间退房,价格的最优(最小)值。


------解决方案--------------------
客房号 入住时间 1小时单价 结算时间 
201 2010-12-01 11:08:32 100 传入值
202 2010-12-01 06:23:44 100 传入值


策略ID 按小时数 有效时间从 至 第N天 时间 价格
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

这两个东东是表+数据,还是什么?
------解决方案--------------------
你这个似乎只要取一下按各个策略算出价格的最小值,再与每小时单价对比即可。
SQL code

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这第二个零是什么意思
------解决方案--------------------
探讨

最终修订版,呵呵
SQL code

--建一个函数,返回最优价格
if object_id('getprice','FN') is not null
drop function getprice
go

--参数分别为入住时间,结算时间,1小时单价
create function getprice(@sdate datetime, @jsdate datetime, @hourpr……