查找连续记录

时间:2018-01-18 07:26:47

标签: tsql

我遵循了代码,该代码应该使用PlanId和costcentreid为我提供连续记录。例如,在图像中,记录号7的costcentreid为14,之前的costcentre为5,因此它将忽略之前的所有记录并返回2017-07-12的StartDate。如果之前的记录是相同的成本中心,那么它将继续回到成本中心不同,然后返回我最低的日期,但它没有这样做。我提供了我的sql。你能帮忙吗?

示例数据:

情景1:正确答案应为' 2017-07-12 11:56:52.560'

   DECLARE @T TABLE (StartDate, PlanId, CostCentreId, PositionId, CostCentreFlavourID, CustomerPositionID)
INSERT @T(StartDate, PLanId, CostCentreID, PositionId, CostCentreFlavourID, CustomerPOsitionID)
VALUES('1998-10-23 00:00:00.000', 19130, 14, 129, 3, 1, 766 ),
('2010-06-22 00:00:00.000', 19130, 207, 25, 3, 1,16247),
('2012-05-01 16:27:04.460', 19130, 42, 14, 3, 1,23946),
('2013-04-30 18:57:57.617', 19130, 295, 14, 3, 1,29453),
('2015-03-03 09:31:28.133', 19130, 275, 5, 3,1, 39286),
('2015-06-26 15:48:35.637', 19130, 195, 5, 3,1,41985),
('2017-07-12 11:56:52.560', 19130, 1445, 14, 3, 1,57699)

场景2:正确答案应该是:' 2015-06-26 15:48:35.637'

DECLARE @T TABLE (StartDate, PlanId, CostCentreId, PositionId, CostCentreFlavourID, CustomerPositionID)
INSERT @T(StartDate, PLanId, CostCentreID, PositionId, CostCentreFlavourID, CustomerPOsitionID)
VALUES('1998-10-23 00:00:00.000', 19130, 14, 129, 3,1,766 ),
('2010-06-22 00:00:00.000', 19130, 207, 25, 3, 1,16247),
('2012-05-01 16:27:04.460', 19130, 42, 14, 3, 1,23946),
('2013-04-30 18:57:57.617', 19130, 295, 14, 3, 1,29453),
('2015-03-03 09:31:28.133', 19130, 275, 5, 3, 1,39286),
('2015-06-26 15:48:35.637', 19130, 195, 14, 3,1, 41985),
('2017-07-12 11:56:52.560', 19130, 1445, 14, 3, 1,57699)

场景3:正确答案应该是:' 2012-05-01 16:27:04.460'

DECLARE @T TABLE (StartDate, PlanId, CostCentreId, PositionId, CostCentreFlavourID, CustomerPositionID)
INSERT @T(StartDate, PLanId, CostCentreID, PositionId, CostCentreFlavourID, CustomerPOsitionID)
VALUES('1998-10-23 00:00:00.000', 19130, 14, 129, 3,1,766 ),
('2010-06-22 00:00:00.000', 19130, 207, 25, 3,1, 16247),
('2012-05-01 16:27:04.460', 19130, 42, 14, 3,1, 23946),
('2013-04-30 18:57:57.617', 19130, 295, 14, 3,1, 29453),
('2015-03-03 09:31:28.133', 19130, 275, 14, 3, 1,39286),
('2015-06-26 15:48:35.637', 19130, 195, 14, 3,1, 41985),
('2017-07-12 11:56:52.560', 19130, 1445, 14, 3,1, 57699)

enter image description here

WITH cte
AS (SELECT cp1.StartDate,
       fp.PlanId,
       p.CostCentreID,
       p.PositionID,
       fp.CostCentreFlavourId,
       fp.CustomerWithNDISNumberOfPlans,
       cp1.CustomerPositionID,
       ROW_NUMBER() OVER (PARTITION BY cp1.CustomerID ORDER BY cp1.StartDate) AS CustomerRow,
       ROW_NUMBER() OVER (PARTITION BY cp1.CustomerID, p.CostCentreID ORDER BY cp1.StartDate) AS CustomerCostCentreRow ,
       ROW_NUMBER() OVER (PARTITION BY cp1.CustomerID ORDER BY cp1.StartDate)
       - ROW_NUMBER() OVER (PARTITION BY cp1.CustomerID, p.CostCentreID ORDER BY cp1.StartDate) rn3
FROM #FlavouredPlans fp
    INNER JOIN dbo.tblCustomerPositions cp1
        ON cp1.CustomerID = fp.LADSCustomerID
    INNER JOIN dbo.tblPositions p
        ON p.PositionID = cp1.PositionID
    --AND fp.CostCentreId = p.CostCentreID
WHERE fp.CostCentreFlavourId = 3
      AND fp.OrderOfPlans = 1
       ),
 ctePositionStartDate
AS (SELECT *,
       MIN(cte.StartDate) OVER (PARTITION BY PlanId, CostCentreID, startdate, rn3) MinStartDate,
       ROW_NUMBER() OVER (PARTITION BY PlanId ORDER BY cte.StartDate ASC) [Order]
FROM cte )

SELECT *
FROM ctePositionStartDate
WHERE ctePositionStartDate.planid = 19130

1 个答案:

答案 0 :(得分:0)

您可以使用窗口功能获得结果。检查此查询

DECLARE @T TABLE (StartDate datetime, PlanId int, CostCentreId int, PositionId int, CostCentreFlavourID int, CustomerPositionID int)
INSERT @T(StartDate, PLanId, PositionId, CostCentreID, CostCentreFlavourID, CustomerPOsitionID)
VALUES
    ('19981023 00:00:00.000', 19130, 14, 129, 3,1),
    ('20100622 00:00:00.000', 19130, 207, 25, 3,1),
    ('20120501 16:27:04.460', 19130, 42, 14, 3,1),
    ('20130430 18:57:57.617', 19130, 295, 12, 3,1),
    ('20150303 09:31:28.133', 19130, 275, 14, 3, 1),
    ('20150626 15:48:35.637', 19130, 195, 14, 3,1),
    ('20170712 11:56:52.560', 19130, 1445, 14, 3,1)


select
    *
from (
    select
        *, row_number() over (partition by PlanId, CostCentreID, rn1 - rn2 order by StartDate) r3
        , max(StartDate) over (partition by PlanId, CostCentreID, rn1 - rn2) mx1
        , max(StartDate) over (partition by PlanId) mx2
    from (
        select 
            *, row_number()over(partition by PlanId order by StartDate) rn1
            , row_number()over(partition by PlanId, CostCentreID order by StartDate) rn2
        from 
            @T
    ) t
) t
where
    mx1 = mx2
    and r3 = 1