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")
提前致谢...希望它不会太混乱。
答案 0 :(得分:0)
不确定我是否了解所有内容,请尝试此操作。
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