2017-02-07 58 views
1

我有一個表格,其中包含與客戶進行團隊諮詢的開始日期和結束日期。按標準分組的非連續日期在列

我需要根據另一列中指定的天數合併某些諮詢(有時諮詢可能重疊,有時它們是連續的,有時它們不是),團隊和類型。

一些示例數據如下:

DECLARE @TempTable TABLE([CUSTOMER_ID] INT 
         ,[TEAM] VARCHAR(1) 
         ,[TYPE] VARCHAR(1) 
         ,[START_DATE] DATETIME 
         ,[END_DATE] DATETIME 
         ,[GROUP_DAYS_CRITERIA] INT) 

INSERT INTO @TempTable VALUES (1,'A','A','2013-08-07','2013-12-31',28) 
          ,(2,'B','A','2015-05-15','2015-05-28',28) 
          ,(2,'B','A','2015-05-15','2016-05-12',28) 
          ,(2,'B','A','2015-05-28','2015-05-28',28) 
          ,(3,'C','A','2013-05-27','2014-07-23',28) 
          ,(3,'C','A','2015-01-12','2015-05-28',28) 
          ,(3,'B','A','2015-01-12','2015-05-28',28) 
          ,(3,'C','A','2015-05-28','2015-05-28',28) 
          ,(3,'C','A','2015-05-28','2015-12-17',28) 
          ,(4,'A','B','2013-07-09','2014-04-21',7) 
          ,(4,'A','B','2014-04-29','2014-08-01',7) 

,看起來像這樣:

+-------------+------+------+------------+------------+---------------------+ 
| CUSTOMER_ID | TEAM | TYPE | START_DATE | END_DATE | GROUP_DAYS_CRITERIA | 
+-------------+------+------+------------+------------+---------------------+ 
|   1 | A | A | 07/08/2013 | 31/12/2013 |     28 | 
|   2 | B | A | 15/05/2015 | 28/05/2015 |     28 | 
|   2 | B | A | 15/05/2015 | 12/05/2016 |     28 | 
|   2 | B | A | 28/05/2015 | 28/05/2015 |     28 | 
|   3 | C | A | 27/05/2013 | 23/07/2014 |     28 | 
|   3 | C | A | 12/01/2015 | 28/05/2015 |     28 | 
|   3 | B | A | 12/01/2015 | 28/05/2015 |     28 | 
|   3 | C | A | 28/05/2015 | 28/05/2015 |     28 | 
|   3 | C | A | 28/05/2015 | 17/12/2015 |     28 | 
|   4 | A | B | 09/07/2013 | 21/04/2014 |     7 | 
|   4 | A | B | 29/04/2014 | 01/08/2014 |     7 | 
+-------------+------+------+------------+------------+---------------------+ 

我的期望輸出如下:

+-------------+------+------+------------+------------+---------------------+ 
| CUSTOMER_ID | TEAM | TYPE | START_DATE | END_DATE | GROUP_DAYS_CRITERIA | 
+-------------+------+------+------------+------------+---------------------+ 
|   1 | A | A | 07/08/2013 | 31/12/2013 |     28 | 
|   2 | B | A | 15/05/2015 | 12/05/2016 |     28 | 
|   3 | C | A | 27/05/2013 | 23/07/2014 |     28 | 
|   3 | C | A | 12/01/2015 | 17/12/2015 |     28 | 
|   3 | B | A | 12/01/2015 | 28/05/2015 |     28 | 
|   4 | A | B | 09/07/2013 | 21/04/2014 |     7 | 
|   4 | A | B | 29/04/2014 | 01/08/2014 |     7 | 
+-------------+------+------+------------+------------+---------------------+ 

我努力做到這一點根本不用提高效率!任何想法/代碼都將大受歡迎。

服務器版本的MS SQL Server 2014

感謝,

+0

我對羣組日期標準應該如何影響分組有點困惑。你能舉一個更好的例子,說明結果是他們爲什麼?團體日標準如何發揮作用? – TTeeple

+0

GROUP_DAYS_CRITERIA列是客戶先前諮詢可以分組的最多天數。因此,如果在第27天(在28天在GROUP_DAYS_CRITERIA列中)進行另一次諮詢,則會進行分組,而29則不會。諮詢小組也必須與其分組類型相同。 –

+0

Answer posted here:http://stackoverflow.com/questions/42223753/group-non-contiguous-dates-by-criteria-in-column-follow-up –

回答

1

如果我正確地理解你的問題,我們要返回行只有當第二,第三等諮詢有沒有發生在上一次諮詢結束日期後的group_days_criteria天數內。

我們可以得到上一個諮詢結束日期,並且在同一個團隊中爲同一客戶進行諮詢並且在我們的日期範圍內諮詢類型相同的情況下排除行(因爲我們不關心諮詢次數)。

DECLARE @TempTable TABLE([CUSTOMER_ID] INT 
        ,[TEAM] VARCHAR(1) 
        ,[TYPE] VARCHAR(1) 
        ,[START_DATE] DATETIME 
        ,[END_DATE] DATETIME 
        ,[GROUP_DAYS_CRITERIA] INT) 

INSERT INTO @TempTable VALUES (1,'A','A','2013-08-07','2013-12-31',28) 
         ,(2,'B','A','2015-05-15','2015-05-28',28) 
         ,(2,'B','A','2015-05-15','2016-05-12',28) 
         ,(2,'B','A','2015-05-28','2015-05-28',28) 
         ,(3,'C','A','2013-05-27','2014-07-23',28) 
         ,(3,'C','A','2015-01-12','2015-05-28',28) 
         ,(3,'B','A','2015-01-12','2015-05-28',28) 
         ,(3,'C','A','2015-05-28','2015-05-28',28) 
         ,(3,'C','A','2015-05-28','2015-12-17',28) 
         ,(4,'A','B','2013-07-09','2014-04-21',7) 
         ,(4,'A','B','2014-04-29','2014-08-01',7) 

;with prep as (
select Customer_ID, 
     Team, 
     [Type], 
     [Start_Date], 
     [End_Date], 
     Group_Days_Criteria, 
     ROW_NUMBER() over (partition by customer_id, team, [type] order by [start_date] asc, [end_date] desc) as rn, -- earliest start date with latest end date 
     lag([End_Date] + Group_Days_Criteria, 1, 0) over (partition by customer_id, team, [type] order by [start_date] asc, [end_date] desc) as PreviousEndDate -- previous end date + 
from @TempTable 
) 

select p.Customer_Id, 
     p.[Team], 
     p.[Type], 
     p.[Start_Date], 
     p.[End_Date], 
     p.Group_Days_Criteria 
from prep p 
where p.rn = 1 
    or (p.rn != 1 and p.[Start_date] > p.PreviousEndDate) 
order by p.Customer_Id, p.[Team], p.[Start_Date], p.[Type] 

這返回了所需的結果集。