0
MatchDate   ColA    ColB    PossibleDate
01/01/2012  ABC     123     01/01/2012
01/01/2012  ABC     123     09/01/2012
01/01/2012  ABC     123     01/15/2013
09/01/2012  ABC     123     01/01/2012
09/01/2012  ABC     123     09/01/2012
09/01/2012  ABC     123     09/13/2012
09/01/2012  ABC     123     10/11/2012
09/01/2012  ABC     123     01/15/2013
01/15/2013  ABC     123     01/01/2012
01/15/2013  ABC     123     09/01/2012
01/15/2013  ABC     123     01/15/2013
01/15/2013  ABC     123     02/22/2013
08/15/2012  XYZ     777     08/15/2012
08/15/2012  XYZ     777     08/28/2012
08/15/2012  XYZ     777     10/11/2012
08/15/2012  XYZ     777     01/15/2013
01/15/2013  XYZ     777     01/15/2013
01/15/2013  XYZ     777     08/15/2012

我正在寻找的结果:

MatchDate   ColA    ColB    PossibleDate
01/01/2012  ABC     123     NULL
09/01/2012  ABC     123     09/13/2012
09/01/2012  ABC     123     10/11/2012
01/15/2013  ABC     123     02/22/2013
08/15/2012  XYZ     777     08/28/2012
08/15/2012  XYZ     777     10/11/2012
01/15/2013  XYZ     777     NULL

它应该为每个唯一的“MatchDate、ColA 和 ColB”列以及介于 MatchDate 和 PossibleDate 之间的任何“可能日期”返回行。

我添加了注释来解释为什么每列有效以及为什么无效:

 MatchDate  ColA    ColB    PossibleDate    (Notes)
01/01/2012  ABC     123     01/01/2012      (Not valid as it's MatchDate already exists for "ABC, 123")
01/01/2012  ABC     123     09/01/2012      (Not valid as it's MatchDate already exists for "ABC, 123")
01/01/2012  ABC     123     01/15/2013      (Not valid as it's MatchDate already exists for "ABC, 123")
09/01/2012  ABC     123     01/01/2012      (Not valid as it's MatchDate already exists for "ABC, 123")
09/01/2012  ABC     123     09/01/2012      (Not valid as it's MatchDate already exists for "ABC, 123")
09/01/2012  ABC     123     09/13/2012      (VALID as it's PossibleDate is NOT a MatchDate and it's between 09/01/2012 and the next possible MatchDate of 01/15/2013 for "ABC, 123")
09/01/2012  ABC     123     10/11/2012      (VALID as it's PossibleDate is NOT a MatchDate and it's between 09/01/2012 and the next possible MatchDate of 01/15/2013 for "ABC, 123")
09/01/2012  ABC     123     01/15/2013      (Not valid as it's MatchDate already exists for "ABC, 123")
01/15/2013  ABC     123     01/01/2012      (Not valid as it's MatchDate already exists for "ABC, 123")
01/15/2013  ABC     123     09/01/2012      (Not valid as it's MatchDate already exists for "ABC, 123")
01/15/2013  ABC     123     01/15/2013      (Not valid as it's MatchDate already exists for "ABC, 123")
01/15/2013  ABC     123     02/22/2013      (VALID as it's PossibleDate is NOT a MatchDate and it's between 01/15/2013 and the next possible MatchDate, which doesn't exist as it's the largest MatchDate for "ABC, 123")
08/15/2012  XYZ     777     08/15/2012      (Not valid as it's MatchDate already exists for "XYZ, 777")
08/15/2012  XYZ     777     08/28/2012      (VALID as it's PossibleDate is NOT a MatchDate and it's between 08/15/2012 and the next possible MatchDate of 01/15/2013 for "XYZ, 777")
08/15/2012  XYZ     777     10/11/2012      (VALID as it's PossibleDate is NOT a MatchDate and it's between 08/15/2012 and the next possible MatchDate of 01/15/2013 for "XYZ, 777")
08/15/2012  XYZ     777     01/15/2013      (Not valid as it's MatchDate already exists for "XYZ, 777")
01/15/2013  XYZ     777     01/15/2013      (Not valid as it's MatchDate already exists for "XYZ, 777")
01/15/2013  XYZ     777     08/15/2012      (VALID as it's PossibleDate is NOT a MatchDate and it's between 01/15/2013 and the next possible MatchDate, which doesn't exist as it's the largest MatchDate for "XYZ, 777")

在此先感谢...希望它不会太混乱。

4

1 回答 1

0

不知道我是否理解所有,请试试这个。

sqlfiddle

WITH cte AS
(SELECT *, 
   NexMatchDate = ISNULL(
       (SELECT MIN(MatchDate) 
        FROM t t2 
        WHERE t2.MatchDate > t.MatchDate
         AND t2.ColA = t.ColA AND t2.ColB = t.ColB)
     ,'99991231')
 FROM t
)
, datelist AS
(SELECT DISTINCT MatchDate, ColA, ColB
 FROM t
)
SELECT d.MatchDate, d.ColA, d.ColB, c.PossibleDate
from datelist d
LEFT JOIN cte c
  ON d.MatchDate = c.MatchDate
  AND d.ColA = c.ColA
  AND d.ColB = c.ColB
  AND c.PossibleDate > c.MatchDate AND c.PossibleDate < c.NexMatchDate
ORDER BY 1,2,3
于 2013-03-12T20:02:33.420 回答