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 に答える