2

我的表中有 400 万行,其中有一个名为 Cancelled Bookings 的空白列,分为 3 年 2010、2011 和 2012

Booking_Skey BookingNumber ArrivalDate DepartureDate BookingDate CancelledDate BookingValue PitchType_Skey Site_Skey
      124532 B00124532      2010-12-31    2011-01-02  2010-12-31          NULL        10.00              7         2

我需要做的是创建一个代码,我可以在其中更改我想要更新的那一年的取消百分比:

因此,对于 2010 年,我需要以下内容

--取消的预订--

  • 8% 的预订在 2010 年被取消,取消日期可以等于或小于到达日期,也可以等于或大于预订日期
  • 8% 中的 20% 在到达日当天取消
  • 8% 中的 20% 在抵达日期前一天取消
  • 8% 中的 20% 在抵达日期前 7 天取消
  • 其余的取消是在 1 到 90 天之间随机分配的

.

USE Occupancy
SELECT ArrivalDate, 
DATEADD(day, 
CASE WHEN Rand(CHECKSUM(NEWID())) BETWEEN 0 and 0.92 THEN NULL ELSE
CASE WHEN Rand(CHECKSUM(NEWID())) BETWEEN 0.92 and 0.94 THEN 0 ELSE
CASE WHEN Rand(CHECKSUM(NEWID())) BETWEEN 0.94 and 0.96 THEN -1 ELSE
CASE WHEN Rand(CHECKSUM(NEWID())) BETWEEN 0.96 and 0.98 THEN -7 ELSE
Round(Rand(CHECKSUM(NEWID())) * -90,0) END END END END, ArrivalDate) AS DaystoReduce 
FROM Bookings
WHERE DATEPART(Year,ArrivalDate) = '2010' and CancelledDate BETWEEN ArrivalDate AND DepartureDate

你能帮我吗?

谢谢

韦恩

4

1 回答 1

0

像下面这样的东西怎么样?它不是很漂亮,所以将其作为练习留给你,但基本上它沿着计算每种类型的数量,然后将规则应用于行号......

declare @shareCancelled float, @shareSameDay float, @sharePrevDay float, @shareSevenDays float
select @shareCancelled = 0.08, @shareSameDay = 0.20, @sharePrevDay = 0.20, @shareSevenDays = 0.20

declare @count int, @cancelled int, @sameDay int, @prevDay int, @sevenDays int
select @count = COUNT(*) from Bookings WHERE DATEPART(Year,ArrivalDate) = '2010' and CancelledDate BETWEEN ArrivalDate AND DepartureDate
select @cancelled = @count * @shareCancelled,
       @sameDay = @count * @shareCancelled * @shareSameDay,
       @prevDay = @count * @shareCancelled * @sharePrevDay,
       @sevenDays = @count * @shareCancelled * @shareSevenDays

select ArrivalDate,
DATEADD(day,
CASE WHEN a.RowNum <= @sameDay THEN 0
     WHEN a.RowNum <= @sameDay + @prevDay THEN -1
     WHEN a.RowNum <= @sameDay + @prevDay + @sevenDays THEN -7
     WHEN a.RowNum <= @cancelled THEN -(ABS(CAST(NEWID() AS binary(6)) %90) + 1)
     ELSE NULL END
, GETDATE()) as DaystoReduce 
from (
  select *, ROW_NUMBER() OVER(ORDER BY NEWID()) as RowNum from Bookings WHERE DATEPART(Year,ArrivalDate) = '2010' and CancelledDate BETWEEN ArrivalDate AND DepartureDate
) as a
于 2013-02-21T17:40:10.587 回答