问题描述:
问题是这样的:一家酒店豪华房的价格分为:平时价和周末价 .周日,周1到周4都是平时价,周五和周六是周末价.请问一下:如果预订这个房间,该怎么才能统计出房间的总价格?比如:预订从2008年的5月份11日到5月21日,该怎么统计出总价格?或者5月份11日到5月26日等等之类的.这样的语句该怎么写呢?知道的朋友说一下.谢谢!
是用SQL语句怎么实现?谢谢!
问题解答:
create proc getprice
@begintime datetime, --开始时间
@endtime datetime , --结束时间
@weekend money, --周末价
@weekday money, --平时价
@num int, --房间数
@sumprice money output --总金额,输出参数
as
declare @date datetime
set @sumprice=0
set @date=@begintime
while(@date<=@endtime) --循环每天
begin
--判断是星期几 小于6是周日到周四 周日是1
if(datepart(weekday,@date)<6)
set @sumprice =@sumprice+money
else
set @sumprice =@sumprice+@weekend
set @date=dateadd(dd,1,@date)
end
set @sumprice=@sumprice*@num
return @sumprice
declare @sumprice money
exec getprice '2008-5-11','2008-5-16',1001,800,2,@sumprice output
print @sumprice
select t.roomNum,sum(s.Money) from
table1 t,
(select roomNum,
(case when
to_char(to_date(orderDate,'yyyy-mm-dd'),'day')
= '星期五' then 10000
when
to_char(to_date(orderDate,'yyyy-mm-dd'),'day')
= '星期六' then 8000
else
5000
end) as Money
from table1 where orderDate between to_date('2008-05-11','yyyy-mm-dd')
and to_date('2008-05-21','yyyy-mm-dd') ) s
where t.roomNum = s.roomNum
group by s.roomNum
--------------------------------------------
不知道你的表结构,你要根据情况改改
------------------------------------------
select roomNum,
(case when
to_char(to_date(orderDate,'yyyy-mm-dd'),'day')
= '星期五' then 10000
when
to_char(to_date(orderDate,'yyyy-mm-dd'),'day')
= '星期六' then 8000
else
5000
end) as Money
from table1 where orderDate between to_date('2008-05-11','yyyy-mm-dd')
and to_date('2008-05-21','yyyy-mm-dd') ) s
-----------------------
这个是得到每天多少价格,星期五是10000,六是 8000 ,其余5000,
外面是统计总价格的,自己根据实际的改改