Tsql 比较连续记录并获取最早的开始日期

Tsql 比较连续记录并获取最早的开始日期,tsql,Tsql,一个客户在其生命周期内可以处于多个职位,并且只能有一个活动职位(由开始日期和结束日期标记)。职位是成本中心的一部分 如果客户在其生命周期内有18个职位,我必须检查连续订单中的任何职位是否属于同一成本中心。如果是,则我使用最早位置的开始日期(相同的成本中心)。我写过这样的东西:通过在稍微不同的分区上使用两个row_number()计算,可以得到一个计算(rn),它允许我们在同一个成本中心为每个连续的职位集分组。在设置临时表时,您已经有一个这样的行号。我包括了rn1和rn2,以便您可以研究它是如何工

一个客户在其生命周期内可以处于多个职位,并且只能有一个活动职位(由开始日期和结束日期标记)。职位是成本中心的一部分

如果客户在其生命周期内有18个职位,我必须检查连续订单中的任何职位是否属于同一成本中心。如果是,则我使用最早位置的开始日期(相同的成本中心)。我写过这样的东西:

通过在稍微不同的分区上使用两个row_number()计算,可以得到一个计算(rn),它允许我们在同一个成本中心为每个连续的职位集分组。在设置临时表时,您已经有一个这样的行号。我包括了rn1和rn2,以便您可以研究它是如何工作的

MS SQL Server 2014架构设置

CREATE TABLE TempTbl
    ([ConsecutivePositions] int, [CustomerID] int, [PositionID] int, [CustomerPositionId] int, [StartDate] datetime, [EndDate] varchar(23), [CostCentreID] int)
;

INSERT INTO TempTbl
    ([ConsecutivePositions], [CustomerID], [PositionID], [CustomerPositionId], [StartDate], [EndDate], [CostCentreID])
VALUES
    (1, 2734, 195, 31860, '2013-10-17 16:08:53', '2015-03-06 11:51:09.440', 5),
    (2, 2734, 29, 39405, '2015-03-06 11:51:09', '2016-01-27 13:10:19.720', 3),
    (3, 2734, 271, 23599, '2012-04-05 16:21:41', '2012-12-04 11:32:47.433', 13),
    (4, 2734, 107, 26479, '2012-12-04 11:32:47', '2013-03-19 09:07:13.633', 14),
    (5, 2734, 297, 28497, '2013-03-19 09:07:13', '2013-10-17 16:08:53.120', 14),
    (6, 2734, 154, 2723, '2007-11-27 00:00:00', '2009-07-10 15:44:16.640', 3),
    (7, 2734, 145, 19436, '2011-03-15 00:00:00', '2011-10-18 15:42:36.877', 906),
    (8, 2734, 146, 17453, '2010-09-12 00:00:00', '2010-11-11 15:58:25.043', 13),
    (9, 2734, 8, 18180, '2010-11-11 00:00:00', '2011-03-15 17:57:48.027', 13),
    (10, 2734, 8, 21606, '2011-10-18 15:42:36', '2011-11-11 16:42:54.787', 13),
    (11, 2734, 8, 21982, '2011-11-14 11:18:24', '2012-04-05 16:21:41.230', 13),
    (12, 2734, 264, 21958, '2011-11-11 16:42:54', '2011-11-14 11:18:24.057', 906),
    (13, 2734, 5, 12785, '2009-07-10 00:00:00', '2009-07-29 09:30:52.430', 3),
    (14, 2734, 5, 12999, '2009-07-29 00:00:00', '2010-03-04 13:00:30.223', 3),
    (15, 2734, 149, 15165, '2010-03-04 00:00:00', '2010-08-16 12:13:30.703', 3),
    (16, 2734, 8, 17044, '2010-08-16 00:00:00', '2010-09-12 16:29:01.203', 13),
    (17, 2734, 891, 45453, '2016-01-27 13:10:19', NULL, 906)
;
with cte as (
    select
        *
        , row_number() over(partition by CustomerID order by StartDate) rn1
        , row_number() over(partition by CustomerID, CostCentreID order by StartDate) rn2
        , row_number() over(partition by CustomerID order by StartDate)
        - row_number() over(partition by CustomerID, CostCentreID order by StartDate) rn3
    from temptbl
    )
select  
       CustomerID
       , CostCentreID
       , rn3
       , count(*) c
       , min(StartDate) StartDate
       , max(EndDate) EndDate
from cte
group by 
       CustomerID, CostCentreID, rn3
order by 
      CustomerID, StartDate
| CustomerID | CostCentreID | rn3 | c |            StartDate |                 EndDate |
|------------|--------------|-----|---|----------------------|-------------------------|
|       2734 |            3 |   0 | 4 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|       2734 |           13 |   4 | 3 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|       2734 |          906 |   7 | 1 | 2011-03-15T00:00:00Z | 2011-10-18 15:42:36.877 |
|       2734 |           13 |   5 | 1 | 2011-10-18T15:42:36Z | 2011-11-11 16:42:54.787 |
|       2734 |          906 |   8 | 1 | 2011-11-11T16:42:54Z | 2011-11-14 11:18:24.057 |
|       2734 |           13 |   6 | 2 | 2011-11-14T11:18:24Z | 2012-12-04 11:32:47.433 |
|       2734 |           14 |  12 | 2 | 2012-12-04T11:32:47Z | 2013-10-17 16:08:53.120 |
|       2734 |            5 |  14 | 1 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |
|       2734 |            3 |  11 | 1 | 2015-03-06T11:51:09Z | 2016-01-27 13:10:19.720 |
|       2734 |          906 |  14 | 1 | 2016-01-27T13:10:19Z |                  (null) |
| ConsecutivePositions | CustomerID | PositionID | CustomerPositionId |            StartDate |                 EndDate | CostCentreID | rn1 | rn2 | rn3 |         MinStartDate |              MaxEndDate |
|----------------------|------------|------------|--------------------|----------------------|-------------------------|--------------|-----|-----|-----|----------------------|-------------------------|
|                    6 |       2734 |        154 |               2723 | 2007-11-27T00:00:00Z | 2009-07-10 15:44:16.640 |            3 |   1 |   1 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   13 |       2734 |          5 |              12785 | 2009-07-10T00:00:00Z | 2009-07-29 09:30:52.430 |            3 |   2 |   2 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   14 |       2734 |          5 |              12999 | 2009-07-29T00:00:00Z | 2010-03-04 13:00:30.223 |            3 |   3 |   3 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   15 |       2734 |        149 |              15165 | 2010-03-04T00:00:00Z | 2010-08-16 12:13:30.703 |            3 |   4 |   4 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   16 |       2734 |          8 |              17044 | 2010-08-16T00:00:00Z | 2010-09-12 16:29:01.203 |           13 |   5 |   1 |   4 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|                    8 |       2734 |        146 |              17453 | 2010-09-12T00:00:00Z | 2010-11-11 15:58:25.043 |           13 |   6 |   2 |   4 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|                    9 |       2734 |          8 |              18180 | 2010-11-11T00:00:00Z | 2011-03-15 17:57:48.027 |           13 |   7 |   3 |   4 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|                   10 |       2734 |          8 |              21606 | 2011-10-18T15:42:36Z | 2011-11-11 16:42:54.787 |           13 |   9 |   4 |   5 | 2011-10-18T15:42:36Z | 2011-11-11 16:42:54.787 |
|                   11 |       2734 |          8 |              21982 | 2011-11-14T11:18:24Z | 2012-04-05 16:21:41.230 |           13 |  11 |   5 |   6 | 2011-11-14T11:18:24Z | 2012-12-04 11:32:47.433 |
|                    3 |       2734 |        271 |              23599 | 2012-04-05T16:21:41Z | 2012-12-04 11:32:47.433 |           13 |  12 |   6 |   6 | 2011-11-14T11:18:24Z | 2012-12-04 11:32:47.433 |
|                    7 |       2734 |        145 |              19436 | 2011-03-15T00:00:00Z | 2011-10-18 15:42:36.877 |          906 |   8 |   1 |   7 | 2011-03-15T00:00:00Z | 2011-10-18 15:42:36.877 |
|                   12 |       2734 |        264 |              21958 | 2011-11-11T16:42:54Z | 2011-11-14 11:18:24.057 |          906 |  10 |   2 |   8 | 2011-11-11T16:42:54Z | 2011-11-14 11:18:24.057 |
|                    2 |       2734 |         29 |              39405 | 2015-03-06T11:51:09Z | 2016-01-27 13:10:19.720 |            3 |  16 |   5 |  11 | 2015-03-06T11:51:09Z | 2016-01-27 13:10:19.720 |
|                    4 |       2734 |        107 |              26479 | 2012-12-04T11:32:47Z | 2013-03-19 09:07:13.633 |           14 |  13 |   1 |  12 | 2012-12-04T11:32:47Z | 2013-10-17 16:08:53.120 |
|                    5 |       2734 |        297 |              28497 | 2013-03-19T09:07:13Z | 2013-10-17 16:08:53.120 |           14 |  14 |   2 |  12 | 2012-12-04T11:32:47Z | 2013-10-17 16:08:53.120 |
|                    1 |       2734 |        195 |              31860 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |            5 |  15 |   1 |  14 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |
|                   17 |       2734 |        891 |              45453 | 2016-01-27T13:10:19Z |                  (null) |          906 |  17 |   3 |  14 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |
查询1

CREATE TABLE TempTbl
    ([ConsecutivePositions] int, [CustomerID] int, [PositionID] int, [CustomerPositionId] int, [StartDate] datetime, [EndDate] varchar(23), [CostCentreID] int)
;

INSERT INTO TempTbl
    ([ConsecutivePositions], [CustomerID], [PositionID], [CustomerPositionId], [StartDate], [EndDate], [CostCentreID])
VALUES
    (1, 2734, 195, 31860, '2013-10-17 16:08:53', '2015-03-06 11:51:09.440', 5),
    (2, 2734, 29, 39405, '2015-03-06 11:51:09', '2016-01-27 13:10:19.720', 3),
    (3, 2734, 271, 23599, '2012-04-05 16:21:41', '2012-12-04 11:32:47.433', 13),
    (4, 2734, 107, 26479, '2012-12-04 11:32:47', '2013-03-19 09:07:13.633', 14),
    (5, 2734, 297, 28497, '2013-03-19 09:07:13', '2013-10-17 16:08:53.120', 14),
    (6, 2734, 154, 2723, '2007-11-27 00:00:00', '2009-07-10 15:44:16.640', 3),
    (7, 2734, 145, 19436, '2011-03-15 00:00:00', '2011-10-18 15:42:36.877', 906),
    (8, 2734, 146, 17453, '2010-09-12 00:00:00', '2010-11-11 15:58:25.043', 13),
    (9, 2734, 8, 18180, '2010-11-11 00:00:00', '2011-03-15 17:57:48.027', 13),
    (10, 2734, 8, 21606, '2011-10-18 15:42:36', '2011-11-11 16:42:54.787', 13),
    (11, 2734, 8, 21982, '2011-11-14 11:18:24', '2012-04-05 16:21:41.230', 13),
    (12, 2734, 264, 21958, '2011-11-11 16:42:54', '2011-11-14 11:18:24.057', 906),
    (13, 2734, 5, 12785, '2009-07-10 00:00:00', '2009-07-29 09:30:52.430', 3),
    (14, 2734, 5, 12999, '2009-07-29 00:00:00', '2010-03-04 13:00:30.223', 3),
    (15, 2734, 149, 15165, '2010-03-04 00:00:00', '2010-08-16 12:13:30.703', 3),
    (16, 2734, 8, 17044, '2010-08-16 00:00:00', '2010-09-12 16:29:01.203', 13),
    (17, 2734, 891, 45453, '2016-01-27 13:10:19', NULL, 906)
;
with cte as (
    select
        *
        , row_number() over(partition by CustomerID order by StartDate) rn1
        , row_number() over(partition by CustomerID, CostCentreID order by StartDate) rn2
        , row_number() over(partition by CustomerID order by StartDate)
        - row_number() over(partition by CustomerID, CostCentreID order by StartDate) rn3
    from temptbl
    )
select  
       CustomerID
       , CostCentreID
       , rn3
       , count(*) c
       , min(StartDate) StartDate
       , max(EndDate) EndDate
from cte
group by 
       CustomerID, CostCentreID, rn3
order by 
      CustomerID, StartDate
| CustomerID | CostCentreID | rn3 | c |            StartDate |                 EndDate |
|------------|--------------|-----|---|----------------------|-------------------------|
|       2734 |            3 |   0 | 4 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|       2734 |           13 |   4 | 3 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|       2734 |          906 |   7 | 1 | 2011-03-15T00:00:00Z | 2011-10-18 15:42:36.877 |
|       2734 |           13 |   5 | 1 | 2011-10-18T15:42:36Z | 2011-11-11 16:42:54.787 |
|       2734 |          906 |   8 | 1 | 2011-11-11T16:42:54Z | 2011-11-14 11:18:24.057 |
|       2734 |           13 |   6 | 2 | 2011-11-14T11:18:24Z | 2012-12-04 11:32:47.433 |
|       2734 |           14 |  12 | 2 | 2012-12-04T11:32:47Z | 2013-10-17 16:08:53.120 |
|       2734 |            5 |  14 | 1 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |
|       2734 |            3 |  11 | 1 | 2015-03-06T11:51:09Z | 2016-01-27 13:10:19.720 |
|       2734 |          906 |  14 | 1 | 2016-01-27T13:10:19Z |                  (null) |
| ConsecutivePositions | CustomerID | PositionID | CustomerPositionId |            StartDate |                 EndDate | CostCentreID | rn1 | rn2 | rn3 |         MinStartDate |              MaxEndDate |
|----------------------|------------|------------|--------------------|----------------------|-------------------------|--------------|-----|-----|-----|----------------------|-------------------------|
|                    6 |       2734 |        154 |               2723 | 2007-11-27T00:00:00Z | 2009-07-10 15:44:16.640 |            3 |   1 |   1 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   13 |       2734 |          5 |              12785 | 2009-07-10T00:00:00Z | 2009-07-29 09:30:52.430 |            3 |   2 |   2 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   14 |       2734 |          5 |              12999 | 2009-07-29T00:00:00Z | 2010-03-04 13:00:30.223 |            3 |   3 |   3 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   15 |       2734 |        149 |              15165 | 2010-03-04T00:00:00Z | 2010-08-16 12:13:30.703 |            3 |   4 |   4 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   16 |       2734 |          8 |              17044 | 2010-08-16T00:00:00Z | 2010-09-12 16:29:01.203 |           13 |   5 |   1 |   4 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|                    8 |       2734 |        146 |              17453 | 2010-09-12T00:00:00Z | 2010-11-11 15:58:25.043 |           13 |   6 |   2 |   4 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|                    9 |       2734 |          8 |              18180 | 2010-11-11T00:00:00Z | 2011-03-15 17:57:48.027 |           13 |   7 |   3 |   4 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|                   10 |       2734 |          8 |              21606 | 2011-10-18T15:42:36Z | 2011-11-11 16:42:54.787 |           13 |   9 |   4 |   5 | 2011-10-18T15:42:36Z | 2011-11-11 16:42:54.787 |
|                   11 |       2734 |          8 |              21982 | 2011-11-14T11:18:24Z | 2012-04-05 16:21:41.230 |           13 |  11 |   5 |   6 | 2011-11-14T11:18:24Z | 2012-12-04 11:32:47.433 |
|                    3 |       2734 |        271 |              23599 | 2012-04-05T16:21:41Z | 2012-12-04 11:32:47.433 |           13 |  12 |   6 |   6 | 2011-11-14T11:18:24Z | 2012-12-04 11:32:47.433 |
|                    7 |       2734 |        145 |              19436 | 2011-03-15T00:00:00Z | 2011-10-18 15:42:36.877 |          906 |   8 |   1 |   7 | 2011-03-15T00:00:00Z | 2011-10-18 15:42:36.877 |
|                   12 |       2734 |        264 |              21958 | 2011-11-11T16:42:54Z | 2011-11-14 11:18:24.057 |          906 |  10 |   2 |   8 | 2011-11-11T16:42:54Z | 2011-11-14 11:18:24.057 |
|                    2 |       2734 |         29 |              39405 | 2015-03-06T11:51:09Z | 2016-01-27 13:10:19.720 |            3 |  16 |   5 |  11 | 2015-03-06T11:51:09Z | 2016-01-27 13:10:19.720 |
|                    4 |       2734 |        107 |              26479 | 2012-12-04T11:32:47Z | 2013-03-19 09:07:13.633 |           14 |  13 |   1 |  12 | 2012-12-04T11:32:47Z | 2013-10-17 16:08:53.120 |
|                    5 |       2734 |        297 |              28497 | 2013-03-19T09:07:13Z | 2013-10-17 16:08:53.120 |           14 |  14 |   2 |  12 | 2012-12-04T11:32:47Z | 2013-10-17 16:08:53.120 |
|                    1 |       2734 |        195 |              31860 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |            5 |  15 |   1 |  14 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |
|                   17 |       2734 |        891 |              45453 | 2016-01-27T13:10:19Z |                  (null) |          906 |  17 |   3 |  14 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |

CREATE TABLE TempTbl
    ([ConsecutivePositions] int, [CustomerID] int, [PositionID] int, [CustomerPositionId] int, [StartDate] datetime, [EndDate] varchar(23), [CostCentreID] int)
;

INSERT INTO TempTbl
    ([ConsecutivePositions], [CustomerID], [PositionID], [CustomerPositionId], [StartDate], [EndDate], [CostCentreID])
VALUES
    (1, 2734, 195, 31860, '2013-10-17 16:08:53', '2015-03-06 11:51:09.440', 5),
    (2, 2734, 29, 39405, '2015-03-06 11:51:09', '2016-01-27 13:10:19.720', 3),
    (3, 2734, 271, 23599, '2012-04-05 16:21:41', '2012-12-04 11:32:47.433', 13),
    (4, 2734, 107, 26479, '2012-12-04 11:32:47', '2013-03-19 09:07:13.633', 14),
    (5, 2734, 297, 28497, '2013-03-19 09:07:13', '2013-10-17 16:08:53.120', 14),
    (6, 2734, 154, 2723, '2007-11-27 00:00:00', '2009-07-10 15:44:16.640', 3),
    (7, 2734, 145, 19436, '2011-03-15 00:00:00', '2011-10-18 15:42:36.877', 906),
    (8, 2734, 146, 17453, '2010-09-12 00:00:00', '2010-11-11 15:58:25.043', 13),
    (9, 2734, 8, 18180, '2010-11-11 00:00:00', '2011-03-15 17:57:48.027', 13),
    (10, 2734, 8, 21606, '2011-10-18 15:42:36', '2011-11-11 16:42:54.787', 13),
    (11, 2734, 8, 21982, '2011-11-14 11:18:24', '2012-04-05 16:21:41.230', 13),
    (12, 2734, 264, 21958, '2011-11-11 16:42:54', '2011-11-14 11:18:24.057', 906),
    (13, 2734, 5, 12785, '2009-07-10 00:00:00', '2009-07-29 09:30:52.430', 3),
    (14, 2734, 5, 12999, '2009-07-29 00:00:00', '2010-03-04 13:00:30.223', 3),
    (15, 2734, 149, 15165, '2010-03-04 00:00:00', '2010-08-16 12:13:30.703', 3),
    (16, 2734, 8, 17044, '2010-08-16 00:00:00', '2010-09-12 16:29:01.203', 13),
    (17, 2734, 891, 45453, '2016-01-27 13:10:19', NULL, 906)
;
with cte as (
    select
        *
        , row_number() over(partition by CustomerID order by StartDate) rn1
        , row_number() over(partition by CustomerID, CostCentreID order by StartDate) rn2
        , row_number() over(partition by CustomerID order by StartDate)
        - row_number() over(partition by CustomerID, CostCentreID order by StartDate) rn3
    from temptbl
    )
select  
       CustomerID
       , CostCentreID
       , rn3
       , count(*) c
       , min(StartDate) StartDate
       , max(EndDate) EndDate
from cte
group by 
       CustomerID, CostCentreID, rn3
order by 
      CustomerID, StartDate
| CustomerID | CostCentreID | rn3 | c |            StartDate |                 EndDate |
|------------|--------------|-----|---|----------------------|-------------------------|
|       2734 |            3 |   0 | 4 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|       2734 |           13 |   4 | 3 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|       2734 |          906 |   7 | 1 | 2011-03-15T00:00:00Z | 2011-10-18 15:42:36.877 |
|       2734 |           13 |   5 | 1 | 2011-10-18T15:42:36Z | 2011-11-11 16:42:54.787 |
|       2734 |          906 |   8 | 1 | 2011-11-11T16:42:54Z | 2011-11-14 11:18:24.057 |
|       2734 |           13 |   6 | 2 | 2011-11-14T11:18:24Z | 2012-12-04 11:32:47.433 |
|       2734 |           14 |  12 | 2 | 2012-12-04T11:32:47Z | 2013-10-17 16:08:53.120 |
|       2734 |            5 |  14 | 1 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |
|       2734 |            3 |  11 | 1 | 2015-03-06T11:51:09Z | 2016-01-27 13:10:19.720 |
|       2734 |          906 |  14 | 1 | 2016-01-27T13:10:19Z |                  (null) |
| ConsecutivePositions | CustomerID | PositionID | CustomerPositionId |            StartDate |                 EndDate | CostCentreID | rn1 | rn2 | rn3 |         MinStartDate |              MaxEndDate |
|----------------------|------------|------------|--------------------|----------------------|-------------------------|--------------|-----|-----|-----|----------------------|-------------------------|
|                    6 |       2734 |        154 |               2723 | 2007-11-27T00:00:00Z | 2009-07-10 15:44:16.640 |            3 |   1 |   1 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   13 |       2734 |          5 |              12785 | 2009-07-10T00:00:00Z | 2009-07-29 09:30:52.430 |            3 |   2 |   2 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   14 |       2734 |          5 |              12999 | 2009-07-29T00:00:00Z | 2010-03-04 13:00:30.223 |            3 |   3 |   3 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   15 |       2734 |        149 |              15165 | 2010-03-04T00:00:00Z | 2010-08-16 12:13:30.703 |            3 |   4 |   4 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   16 |       2734 |          8 |              17044 | 2010-08-16T00:00:00Z | 2010-09-12 16:29:01.203 |           13 |   5 |   1 |   4 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|                    8 |       2734 |        146 |              17453 | 2010-09-12T00:00:00Z | 2010-11-11 15:58:25.043 |           13 |   6 |   2 |   4 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|                    9 |       2734 |          8 |              18180 | 2010-11-11T00:00:00Z | 2011-03-15 17:57:48.027 |           13 |   7 |   3 |   4 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|                   10 |       2734 |          8 |              21606 | 2011-10-18T15:42:36Z | 2011-11-11 16:42:54.787 |           13 |   9 |   4 |   5 | 2011-10-18T15:42:36Z | 2011-11-11 16:42:54.787 |
|                   11 |       2734 |          8 |              21982 | 2011-11-14T11:18:24Z | 2012-04-05 16:21:41.230 |           13 |  11 |   5 |   6 | 2011-11-14T11:18:24Z | 2012-12-04 11:32:47.433 |
|                    3 |       2734 |        271 |              23599 | 2012-04-05T16:21:41Z | 2012-12-04 11:32:47.433 |           13 |  12 |   6 |   6 | 2011-11-14T11:18:24Z | 2012-12-04 11:32:47.433 |
|                    7 |       2734 |        145 |              19436 | 2011-03-15T00:00:00Z | 2011-10-18 15:42:36.877 |          906 |   8 |   1 |   7 | 2011-03-15T00:00:00Z | 2011-10-18 15:42:36.877 |
|                   12 |       2734 |        264 |              21958 | 2011-11-11T16:42:54Z | 2011-11-14 11:18:24.057 |          906 |  10 |   2 |   8 | 2011-11-11T16:42:54Z | 2011-11-14 11:18:24.057 |
|                    2 |       2734 |         29 |              39405 | 2015-03-06T11:51:09Z | 2016-01-27 13:10:19.720 |            3 |  16 |   5 |  11 | 2015-03-06T11:51:09Z | 2016-01-27 13:10:19.720 |
|                    4 |       2734 |        107 |              26479 | 2012-12-04T11:32:47Z | 2013-03-19 09:07:13.633 |           14 |  13 |   1 |  12 | 2012-12-04T11:32:47Z | 2013-10-17 16:08:53.120 |
|                    5 |       2734 |        297 |              28497 | 2013-03-19T09:07:13Z | 2013-10-17 16:08:53.120 |           14 |  14 |   2 |  12 | 2012-12-04T11:32:47Z | 2013-10-17 16:08:53.120 |
|                    1 |       2734 |        195 |              31860 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |            5 |  15 |   1 |  14 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |
|                   17 |       2734 |        891 |              45453 | 2016-01-27T13:10:19Z |                  (null) |          906 |  17 |   3 |  14 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |
---- 使用更多的窗口函数,而不是按查询分组,您可以获得临时表中的所有详细信息以及所需的成本中心相关日期

with cte as (
    select
        *
        , row_number() over(partition by CustomerID order by StartDate) rn1
        , row_number() over(partition by CustomerID, CostCentreID order by StartDate) rn2
        , row_number() over(partition by CustomerID order by StartDate)
        - row_number() over(partition by CustomerID, CostCentreID order by StartDate) rn3
    from temptbl
    )
, cte2 as (
      select  
             *
             , min(StartDate) over(partition by CustomerID, CostCentreID, rn3) MinStartDate
             , max(EndDate) over(partition by CustomerID, CostCentreID, rn3) MaxEndDate
      from cte
     )
select
*
from cte2
;

CREATE TABLE TempTbl
    ([ConsecutivePositions] int, [CustomerID] int, [PositionID] int, [CustomerPositionId] int, [StartDate] datetime, [EndDate] varchar(23), [CostCentreID] int)
;

INSERT INTO TempTbl
    ([ConsecutivePositions], [CustomerID], [PositionID], [CustomerPositionId], [StartDate], [EndDate], [CostCentreID])
VALUES
    (1, 2734, 195, 31860, '2013-10-17 16:08:53', '2015-03-06 11:51:09.440', 5),
    (2, 2734, 29, 39405, '2015-03-06 11:51:09', '2016-01-27 13:10:19.720', 3),
    (3, 2734, 271, 23599, '2012-04-05 16:21:41', '2012-12-04 11:32:47.433', 13),
    (4, 2734, 107, 26479, '2012-12-04 11:32:47', '2013-03-19 09:07:13.633', 14),
    (5, 2734, 297, 28497, '2013-03-19 09:07:13', '2013-10-17 16:08:53.120', 14),
    (6, 2734, 154, 2723, '2007-11-27 00:00:00', '2009-07-10 15:44:16.640', 3),
    (7, 2734, 145, 19436, '2011-03-15 00:00:00', '2011-10-18 15:42:36.877', 906),
    (8, 2734, 146, 17453, '2010-09-12 00:00:00', '2010-11-11 15:58:25.043', 13),
    (9, 2734, 8, 18180, '2010-11-11 00:00:00', '2011-03-15 17:57:48.027', 13),
    (10, 2734, 8, 21606, '2011-10-18 15:42:36', '2011-11-11 16:42:54.787', 13),
    (11, 2734, 8, 21982, '2011-11-14 11:18:24', '2012-04-05 16:21:41.230', 13),
    (12, 2734, 264, 21958, '2011-11-11 16:42:54', '2011-11-14 11:18:24.057', 906),
    (13, 2734, 5, 12785, '2009-07-10 00:00:00', '2009-07-29 09:30:52.430', 3),
    (14, 2734, 5, 12999, '2009-07-29 00:00:00', '2010-03-04 13:00:30.223', 3),
    (15, 2734, 149, 15165, '2010-03-04 00:00:00', '2010-08-16 12:13:30.703', 3),
    (16, 2734, 8, 17044, '2010-08-16 00:00:00', '2010-09-12 16:29:01.203', 13),
    (17, 2734, 891, 45453, '2016-01-27 13:10:19', NULL, 906)
;
with cte as (
    select
        *
        , row_number() over(partition by CustomerID order by StartDate) rn1
        , row_number() over(partition by CustomerID, CostCentreID order by StartDate) rn2
        , row_number() over(partition by CustomerID order by StartDate)
        - row_number() over(partition by CustomerID, CostCentreID order by StartDate) rn3
    from temptbl
    )
select  
       CustomerID
       , CostCentreID
       , rn3
       , count(*) c
       , min(StartDate) StartDate
       , max(EndDate) EndDate
from cte
group by 
       CustomerID, CostCentreID, rn3
order by 
      CustomerID, StartDate
| CustomerID | CostCentreID | rn3 | c |            StartDate |                 EndDate |
|------------|--------------|-----|---|----------------------|-------------------------|
|       2734 |            3 |   0 | 4 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|       2734 |           13 |   4 | 3 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|       2734 |          906 |   7 | 1 | 2011-03-15T00:00:00Z | 2011-10-18 15:42:36.877 |
|       2734 |           13 |   5 | 1 | 2011-10-18T15:42:36Z | 2011-11-11 16:42:54.787 |
|       2734 |          906 |   8 | 1 | 2011-11-11T16:42:54Z | 2011-11-14 11:18:24.057 |
|       2734 |           13 |   6 | 2 | 2011-11-14T11:18:24Z | 2012-12-04 11:32:47.433 |
|       2734 |           14 |  12 | 2 | 2012-12-04T11:32:47Z | 2013-10-17 16:08:53.120 |
|       2734 |            5 |  14 | 1 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |
|       2734 |            3 |  11 | 1 | 2015-03-06T11:51:09Z | 2016-01-27 13:10:19.720 |
|       2734 |          906 |  14 | 1 | 2016-01-27T13:10:19Z |                  (null) |
| ConsecutivePositions | CustomerID | PositionID | CustomerPositionId |            StartDate |                 EndDate | CostCentreID | rn1 | rn2 | rn3 |         MinStartDate |              MaxEndDate |
|----------------------|------------|------------|--------------------|----------------------|-------------------------|--------------|-----|-----|-----|----------------------|-------------------------|
|                    6 |       2734 |        154 |               2723 | 2007-11-27T00:00:00Z | 2009-07-10 15:44:16.640 |            3 |   1 |   1 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   13 |       2734 |          5 |              12785 | 2009-07-10T00:00:00Z | 2009-07-29 09:30:52.430 |            3 |   2 |   2 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   14 |       2734 |          5 |              12999 | 2009-07-29T00:00:00Z | 2010-03-04 13:00:30.223 |            3 |   3 |   3 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   15 |       2734 |        149 |              15165 | 2010-03-04T00:00:00Z | 2010-08-16 12:13:30.703 |            3 |   4 |   4 |   0 | 2007-11-27T00:00:00Z | 2010-08-16 12:13:30.703 |
|                   16 |       2734 |          8 |              17044 | 2010-08-16T00:00:00Z | 2010-09-12 16:29:01.203 |           13 |   5 |   1 |   4 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|                    8 |       2734 |        146 |              17453 | 2010-09-12T00:00:00Z | 2010-11-11 15:58:25.043 |           13 |   6 |   2 |   4 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|                    9 |       2734 |          8 |              18180 | 2010-11-11T00:00:00Z | 2011-03-15 17:57:48.027 |           13 |   7 |   3 |   4 | 2010-08-16T00:00:00Z | 2011-03-15 17:57:48.027 |
|                   10 |       2734 |          8 |              21606 | 2011-10-18T15:42:36Z | 2011-11-11 16:42:54.787 |           13 |   9 |   4 |   5 | 2011-10-18T15:42:36Z | 2011-11-11 16:42:54.787 |
|                   11 |       2734 |          8 |              21982 | 2011-11-14T11:18:24Z | 2012-04-05 16:21:41.230 |           13 |  11 |   5 |   6 | 2011-11-14T11:18:24Z | 2012-12-04 11:32:47.433 |
|                    3 |       2734 |        271 |              23599 | 2012-04-05T16:21:41Z | 2012-12-04 11:32:47.433 |           13 |  12 |   6 |   6 | 2011-11-14T11:18:24Z | 2012-12-04 11:32:47.433 |
|                    7 |       2734 |        145 |              19436 | 2011-03-15T00:00:00Z | 2011-10-18 15:42:36.877 |          906 |   8 |   1 |   7 | 2011-03-15T00:00:00Z | 2011-10-18 15:42:36.877 |
|                   12 |       2734 |        264 |              21958 | 2011-11-11T16:42:54Z | 2011-11-14 11:18:24.057 |          906 |  10 |   2 |   8 | 2011-11-11T16:42:54Z | 2011-11-14 11:18:24.057 |
|                    2 |       2734 |         29 |              39405 | 2015-03-06T11:51:09Z | 2016-01-27 13:10:19.720 |            3 |  16 |   5 |  11 | 2015-03-06T11:51:09Z | 2016-01-27 13:10:19.720 |
|                    4 |       2734 |        107 |              26479 | 2012-12-04T11:32:47Z | 2013-03-19 09:07:13.633 |           14 |  13 |   1 |  12 | 2012-12-04T11:32:47Z | 2013-10-17 16:08:53.120 |
|                    5 |       2734 |        297 |              28497 | 2013-03-19T09:07:13Z | 2013-10-17 16:08:53.120 |           14 |  14 |   2 |  12 | 2012-12-04T11:32:47Z | 2013-10-17 16:08:53.120 |
|                    1 |       2734 |        195 |              31860 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |            5 |  15 |   1 |  14 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |
|                   17 |       2734 |        891 |              45453 | 2016-01-27T13:10:19Z |                  (null) |          906 |  17 |   3 |  14 | 2013-10-17T16:08:53Z | 2015-03-06 11:51:09.440 |

请提供一些样本数据,您的临时表的内容可能就是现在问题中需要提供的全部内容。不知道提供样本数据的其他方式。现在由您决定。谢谢。非常感谢。在窗口函数中,您错过了分区函数中的costcentreid,这就是它返回错误结果的原因。您能解释一下您所做的吗?非常感谢。这是可行的,但我并不完全理解。请解释这是我的解释。在cte内运行查询并查看
rn1
rn2
rn3
值,仔细查看
rn1
rn2
的分区。对于成本中心的每次变化,两个数字之间的差异是恒定的,一旦我们将其作为一个常数,用它来分组为什么要减去得到rn3?我还需要customerpositionid列。可以更改吗?我还需要customerpositionid列。有可能改变吗。如果您将positionid引入到组中,从而打乱了日期计算,则必须重新加入临时表。你能做到的。