我這個問題,回答了這個職位,但都被告知要重新問這是一個新的問題:集團非連續日期按條件在列(跟進)
Group Non-Contiguous Dates By Criteria In Column
然而,我已經注意到,雖然解決方案大部分時間都在工作,但是有些錯誤會慢慢滲透到無法正確分組的地方。
例如,
數據:
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)
,(5,'A','A','2014-05-15','2015-04-24',28)
,(5,'A','A','2014-05-15','2015-04-24',28)
,(5,'A','A','2014-05-15','2014-05-15',28)
,(5,'A','A','2015-04-24','2015-05-13',28)
,(5,'A','B','2014-05-15','2014-05-15',7)
,(5,'A','B','2014-06-13','2015-04-24',7)
,(5,'A','B','2014-06-13','2015-04-24',7)
,(5,'A','B','2015-04-24','2015-05-13',7)
,(6,'A','A','2015-02-17','2015-04-28',28)
,(6,'A','A','2015-02-17','2015-04-28',28)
,(6,'A','A','2015-04-10','2015-04-28',28)
,(6,'A','A','2015-04-10','2015-04-28',28)
,(6,'A','A','2015-04-28','2015-06-04',28)
,(6,'A','A','2015-04-28','2015-08-03',28)
,(6,'A','A','2015-05-22','2015-08-03',28)
,(7,'A','A','2015-03-30','2015-04-28',28)
,(7,'A','A','2015-03-30','2015-04-28',28)
,(7,'A','A','2015-03-30','2015-04-28',28)
,(7,'A','A','2015-03-30','2015-04-28',28)
,(7,'A','A','2015-04-28','2015-11-17',28)
,(7,'A','A','2015-04-28','2015-11-17',28)
,(7,'A','A','2015-05-12','2015-11-17',28)
,(7,'A','A','2015-05-12','2015-11-17',28)
,看起來像這樣:
+-------------+------+------+------------+------------+---------------------+
| 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 |
| 5 | A | A | 15/05/2014 | 24/04/2015 | 28 |
| 5 | A | A | 15/05/2014 | 24/04/2015 | 28 |
| 5 | A | A | 15/05/2014 | 15/05/2014 | 28 |
| 5 | A | A | 24/04/2015 | 13/05/2015 | 28 |
| 5 | A | B | 15/05/2014 | 15/05/2014 | 7 |
| 5 | A | B | 13/06/2014 | 24/04/2015 | 7 |
| 5 | A | B | 13/06/2014 | 24/04/2015 | 7 |
| 5 | A | B | 24/04/2015 | 13/05/2015 | 7 |
| 6 | A | A | 17/02/2015 | 28/04/2015 | 28 |
| 6 | A | A | 17/02/2015 | 28/04/2015 | 28 |
| 6 | A | A | 10/04/2015 | 28/04/2015 | 28 |
| 6 | A | A | 10/04/2015 | 28/04/2015 | 28 |
| 6 | A | A | 28/04/2015 | 04/06/2015 | 28 |
| 6 | A | A | 28/04/2015 | 03/08/2015 | 28 |
| 6 | A | A | 22/05/2015 | 03/08/2015 | 28 |
| 7 | A | A | 30/03/2015 | 28/04/2015 | 28 |
| 7 | A | A | 30/03/2015 | 28/04/2015 | 28 |
| 7 | A | A | 30/03/2015 | 28/04/2015 | 28 |
| 7 | A | A | 30/03/2015 | 28/04/2015 | 28 |
| 7 | A | A | 28/04/2015 | 17/11/2015 | 28 |
| 7 | A | A | 28/04/2015 | 17/11/2015 | 28 |
| 7 | A | A | 12/05/2015 | 17/11/2015 | 28 |
| 7 | A | A | 12/05/2015 | 17/11/2015 | 28 |
+-------------+------+------+------------+------------+---------------------+
這是目前出來是這樣的:
+-------------+------+------+------------+------------+---------------------+
| 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 | B | A | 12/01/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 |
| 4 | A | B | 09/07/2013 | 21/04/2014 | 7 |
| 4 | A | B | 29/04/2014 | 01/08/2014 | 7 |
| 5 | A | A | 15/05/2014 | 24/04/2015 | 28 |
| 5 | A | B | 15/05/2014 | 15/05/2014 | 7 |
| 5 | A | B | 13/06/2014 | 24/04/2015 | 7 |
| 5 | A | A | 24/04/2015 | 13/05/2015 | 28 |
| 6 | A | A | 17/02/2015 | 28/04/2015 | 28 |
| 7 | A | A | 30/03/2015 | 28/04/2015 | 28 |
+-------------+------+------+------------+------------+---------------------+
我需要它像t一樣出來他的:
+-------------+------+------+------------+------------+---------------------+
| 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 |
| 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 |
| 5 | A | A | 15/05/2014 | 13/05/2015 | 28 |
| 5 | A | B | 15/05/2014 | 15/05/2014 | 7 |
| 5 | A | B | 13/06/2014 | 13/05/2015 | 7 |
| 6 | A | A | 17/02/2015 | 03/08/2015 | 28 |
| 7 | A | A | 30/03/2015 | 17/11/2015 | 28 |
+-------------+------+------+------------+------------+---------------------+
有關如何解決這個問題的任何想法,同時仍然保持正確輸出的標準?
丹尼爾
什麼是解決重複的球隊/類型對邏輯? –
@TimBiegeleisen在結果的第一行和最後一行在日期term中是連續的。第一行在'24/04/2015'結束,在第四行結束,持續到2015年5月13日 – Mihai
您應該問誰給你第一個幫助的答案。 –