假日可用性日历 - 可用天数仍然连续几天出售

时间:2015-06-11 09:51:04

标签: calendar max min availability

我要求的是一个小时&可供出售的BaseDate的最大值= 1且仍有3个或更多连续日可供出售。但是,如果属性changeoverday在BaseDate的同一天开始,则需要排除总和,因为我们只对由于转换限制而无法出售的差距感兴趣。数据必须按代码分组,因为我们有超过1,000个属性。 BaseDates适用于2015年和2015年。 2016。

注意:有些房产有超过1次转换日和时间。目前以逗号分隔,即星期六,星期日

示例数据: -

DECLARE @sampleData TABLE (
  Code VARCHAR(5) NOT NULL
, BaseDate DATE  NOT NULL
, DayName VARCHAR(9) NOT NULL
, ChangeoverDay VARCHAR(8) NOT NULL
, AvailabletoSell BIT  NOT NULL
);

INSERT INTO @sampleData VALUES 
('PERCH','2015-05-06','Wednesday','Saturday',0),
('PERCH','2015-05-07','Thursday','Saturday',0),
('PERCH','2015-05-08','Friday','Saturday',0),
('PERCH','2015-05-09','Saturday','Saturday',1), -- Not this one as changeover day is the same as the BaseDate

('PERCH','2015-05-10','Sunday','Saturday',1),
('PERCH','2015-05-11','Monday','Saturday',1),
('PERCH','2015-05-12','Tuesday','Saturday',0),
('PERCH','2015-05-13','Wednesday','Saturday',0),
('PERCH','2015-05-14','Thursday','Saturday',1), -- This one = 3

('PERCH','2015-05-15','Friday','Saturday',1),
('PERCH','2015-05-16','Saturday','Saturday',1),
('PERCH','2015-05-17','Sunday','Saturday',0),
('PERCH','2015-05-18','Monday','Saturday',1), -- This one = 4

('PERCH','2015-05-19','Tuesday','Saturday',1),
('PERCH','2015-05-20','Wednesday','Saturday',1),
('PERCH','2015-05-21','Thursday','Saturday',1),
('PERCH','2015-05-22','Friday','Saturday',0),
('PERCH','2015-05-23','Saturday','Saturday',0),
('PERCH','2015-05-24','Sunday','Saturday',0),
('PERCH','2015-05-25','Monday','Saturday',0),
('PERCH','2015-05-26','Tuesday','Saturday',0),
('PERCH','2015-05-27','Wednesday','Saturday',1), -- Not this one, as only 2 consecutive days
('PERCH','2015-05-28','Thursday','Saturday',1),
('PERCH','2015-05-29','Friday','Saturday',0),
('PERCH','2015-05-30','Saturday','Saturday',0);

我需要输出如下: -

+-------+---------------+-------------+----------------------+
| Code  | StartBaseDate | EndBaseDate | TotalAvailabletoSell |
+-------+---------------+-------------+----------------------+
| PERCH | 14/05/2015    | 16/05/2015  |                    3 |
| PERCH | 18/05/2015    | 21/05/2015  |                    4 |
+-------+---------------+-------------+----------------------+

1 个答案:

答案 0 :(得分:1)

这可以满足您的需求。但我觉得有办法减少接触表格的次数

WITH Groupings AS (

    SELECT
        Code
        ,LastChange
        ,MIN(BaseDate)  AS StartBaseDate 
        ,MAX(BaseDate)  AS EndBaseDate
        ,COUNT(*)       AS DaysInPeriod
    FROM
        @sampleData AS s1
        CROSS APPLY (
            SELECT
                MAX(BaseDate) AS LastChange
            FROM
                @sampleData AS cv
            WHERE
                s1.BaseDate > cv.BaseDate
            AND s1.AvailabletoSell != cv.AvailabletoSell
            AND s1.Code = cv.Code
        ) AS cv
    WHERE
        s1.AvailabletoSell = 1
    GROUP BY
        Code
        ,LastChange
)
SELECT
    g.Code
    ,g.StartBaseDate
    ,g.EndBaseDate
    ,CASE WHEN a.DayName = a.ChangeoverDay THEN DaysInPeriod - 1 ELSE DaysInPeriod END  AS TotalAvailableToSell
FROM
    Groupings AS g
    INNER JOIN @sampleData AS a 
        ON a.BaseDate = g.StartBaseDate AND a.Code = g.Code
WHERE
    CASE WHEN a.DayName = a.ChangeoverDay THEN DaysInPeriod - 1 ELSE DaysInPeriod END > 2

逻辑非常多:

  • 在“this row”
  • 之前找到AvailableToSell标志翻转的最后日期
  • 按这些日期分组,并计算其中的行数
  • 如果开始日期将DayName作为ChangeoverDay
  • ,则减1

我没有考虑你关于ChangeoverDay是逗号分隔字段的说明。有很多资源可以打破你可以加入的资源。但我认为你还需要扩展在这个场景中发生的关于DayName在ChangeoverDays列表中的内容