1

我需要的是 BaseDate 的最小值和最大值,其中可出售 = 1 并且连续 3 天或更多天仍可出售。但是,如果属性转换日与 BaseDate 同一天开始,则需要排除总和,因为我们只对由于转换限制而无法出售的间隙感兴趣。数据必须按代码分组,因为我们有 1,000 多个属性。基准日期为 2015 年和 2016 年。

注意:一些属性有超过 1 个 changeoverDay 并且当前以逗号分隔的一列举行,即星期六、星期日

示例数据:-

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 |
+-------+---------------+-------------+----------------------+
4

1 回答 1

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

逻辑差不多:

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

我没有考虑到您关于 ChangeoverDay 是逗号分隔字段的注释。有很多资源可以打破这些资源,然后您可以加入。但我认为你还需要扩展在这种情况下发生的事情,关于 DayName 在 ChangeoverDays 列表中

于 2015-06-12T12:13:30.673 回答