我已经看到很多解决方案来识别日期范围重叠的记录,还有其他合并重叠范围的例子。
但是我对显示仅发生重叠的范围的结果感兴趣。事实上,我有3个ProductID(并且只有3个将存在),我正在尝试为每个客户找到他们拥有所有这三个时的日期范围。
SET NOCOUNT ON;
CREATE TABLE #tmp
(
CustomerID integer
,ProductID varchar(12)
,Eff_Dt DATE
,End_Dt DATE
);
-- Customer 1000: Expecting results to show 2 rows: 1/1 - 1/5 and 1/10 - 1/15
INSERT INTO #tmp VALUES (1000,'PRODUCT_A','01-01-2013' ,'01-31-2013' );
INSERT INTO #tmp VALUES (1000,'PRODUCT_B','01-01-2013' ,'01-05-2013' );
INSERT INTO #tmp VALUES (1000,'PRODUCT_B','01-10-2013' ,'01-15-2013' );
INSERT INTO #tmp VALUES (1000,'PRODUCT_C','01-01-2013' ,'01-31-2013' );
-- Customer 2000: Expecting results to show 1 row: 1/19 - 1/31
INSERT INTO #tmp VALUES (2000,'PRODUCT_A','01-01-2013' ,'01-31-2013' );
INSERT INTO #tmp VALUES (2000,'PRODUCT_B','01-01-2013' ,'01-31-2013' );
INSERT INTO #tmp VALUES (2000,'PRODUCT_C','01-19-2013' ,'01-31-2013' );
-- Customer 3000: Expecting results to show no rows (or nulls)
INSERT INTO #tmp VALUES (3000,'PRODUCT_A','01-01-2013' ,'01-10-2013' );
INSERT INTO #tmp VALUES (3000,'PRODUCT_A','01-16-2013' ,'01-31-2013' );
INSERT INTO #tmp VALUES (3000,'PRODUCT_B','01-01-2013' ,'01-12-2013' );
INSERT INTO #tmp VALUES (3000,'PRODUCT_C','01-15-2013' ,'01-31-2013' );
-- Customer 4000: Expecting results to show 1 row: 1/15 - 1/23
INSERT INTO #tmp VALUES (4000,'PRODUCT_A','01-15-2013' ,'01-31-2013' );
INSERT INTO #tmp VALUES (4000,'PRODUCT_B','01-01-2013' ,'01-31-2013' );
INSERT INTO #tmp VALUES (4000,'PRODUCT_C','01-01-2013' ,'01-23-2013' );
-- Customer 5000: Expecting results to show 0 rows
INSERT INTO #tmp VALUES (5000,'PRODUCT_A','01-17-2013' ,'01-31-2013' );
INSERT INTO #tmp VALUES (5000,'PRODUCT_B','01-01-2013' ,'01-10-2013' );
INSERT INTO #tmp VALUES (5000,'PRODUCT_C','01-07-2013' ,'01-19-2013' );
-- Customer 6000: Expecting results to show 3 rows: 1/11 - 1/12 1/17 - 1/22 1/26 - 1/27
INSERT INTO #tmp VALUES (6000,'PRODUCT_A','01-01-2013' ,'01-04-2013' );
INSERT INTO #tmp VALUES (6000,'PRODUCT_A','01-09-2013' ,'01-12-2013' );
INSERT INTO #tmp VALUES (6000,'PRODUCT_A','01-17-2013' ,'01-22-2013' );
INSERT INTO #tmp VALUES (6000,'PRODUCT_A','01-26-2013' ,'01-31-2013' );
INSERT INTO #tmp VALUES (6000,'PRODUCT_B','01-04-2013' ,'01-28-2013' );
INSERT INTO #tmp VALUES (6000,'PRODUCT_C','01-11-2013' ,'01-27-2013' );
SET NOCOUNT OFF;
/* ====== EXPECTED RESULTS =======================
CustomerID EFF_DT END_DT
1000 1/1/2013 1/5/2013
1000 1/10/2013 1/15/2013
2000 1/19/2013 1/31/2013
4000 1/15/2013 1/23/2013
6000 1/11/2013 1/12/2013
6000 1/17/2013 1/22/2013
6000 1/26/2013 1/27/2013
===================================================*/
答案 0 :(得分:4)
以下是答案:
select t.customerid, t.eff_dt, count(distinct t2.productId),
MIN(t2.end_dt) as end_dt
from #tmp t join
#tmp t2
on t.CustomerID = t2.CustomerID and
t.Eff_Dt between t2.Eff_Dt and t2.End_Dt
group by t.CustomerID, t.eff_dt
having count(distinct t2.productId) = 3
这是使用自联接来计算每个eff_dt
上不同产品的数量。您需要三个不同的产品,这就是having
子句正在做的事情。
有三种不同的产品,直到其中一种产品结束。这将是生效日期之后的第一个end_dt
- 由min(end_dt)
计算。
答案 1 :(得分:1)
尝试:
select ab.CustomerID,
case when ab_Eff_Dt > c.Eff_Dt then ab_Eff_Dt else c.Eff_Dt end abc_Eff_Dt,
case when ab_End_Dt < c.End_Dt then ab_End_Dt else c.End_Dt end abc_End_Dt
from
(select a.CustomerID,
case when a.Eff_Dt > b.Eff_Dt then a.Eff_Dt else b.Eff_Dt end ab_Eff_Dt,
case when a.End_Dt < b.End_Dt then a.End_Dt else b.End_Dt end ab_End_Dt
from #tmp a
join #tmp b
on a.CustomerID = b.CustomerID and a.Eff_Dt < b.End_Dt and b.Eff_Dt < a.End_Dt
where a.ProductID = 'PRODUCT_A' and b.ProductID = 'PRODUCT_B') ab
join #tmp c
on ab.CustomerID = c.CustomerID and ab_Eff_Dt < c.End_Dt and c.Eff_Dt < ab_End_Dt
where c.ProductID = 'PRODUCT_C'
(SQLFiddle here)
答案 2 :(得分:0)
CREATE TABLE #TMP
(
CustomerID INT,
ProductID VARCHAR(12),
Eff_Dt DATE,
End_Dt DATE
);
--
INSERT INTO #TMP
VALUES
-- Customer 1000: Expecting results to show 2 rows: "1/1 - 1/5"; "1/10 - 1/15"
(1000, 'PRODUCT_A', N'2013-01-01T00:00:00', N'2013-01-31T00:00:00'),
(1000, 'PRODUCT_B', N'2013-01-01T00:00:00', N'2013-01-05T00:00:00'),
(1000, 'PRODUCT_B', N'2013-01-10T00:00:00', N'2013-01-15T00:00:00'),
(1000, 'PRODUCT_C', N'2013-01-01T00:00:00', N'2013-01-31T00:00:00'),
-- Customer 2000: Expecting results to show 1 row: "1/19 - 1/31"
(2000, 'PRODUCT_A', N'2013-01-01T00:00:00', N'2013-01-31T00:00:00'),
(2000, 'PRODUCT_B', N'2013-01-01T00:00:00', N'2013-01-31T00:00:00'),
(2000, 'PRODUCT_C', N'2013-01-19T00:00:00', N'2013-01-31T00:00:00'),
-- Customer 3000: Expecting results to show NO rows (or nulls)
(3000, 'PRODUCT_A', N'2013-01-01T00:00:00', N'2013-01-10T00:00:00'),
(3000, 'PRODUCT_A', N'2013-01-16T00:00:00', N'2013-01-31T00:00:00'),
(3000, 'PRODUCT_B', N'2013-01-01T00:00:00', N'2013-01-12T00:00:00'),
(3000, 'PRODUCT_C', N'2013-01-15T00:00:00', N'2013-01-31T00:00:00'),
-- Customer 4000: Expecting results to show 1 row: "1/15 - 1/23"
(4000, 'PRODUCT_A', N'2013-01-15T00:00:00', N'2013-01-31T00:00:00'),
(4000, 'PRODUCT_B', N'2013-01-01T00:00:00', N'2013-01-31T00:00:00'),
(4000, 'PRODUCT_C', N'2013-01-01T00:00:00', N'2013-01-23T00:00:00'),
-- Customer 5000: Expecting results to show 0 rows
(5000, 'PRODUCT_A', N'2013-01-17T00:00:00', N'2013-01-31T00:00:00'),
(5000, 'PRODUCT_B', N'2013-01-01T00:00:00', N'2013-01-10T00:00:00'),
(5000, 'PRODUCT_C', N'2013-01-07T00:00:00', N'2013-01-19T00:00:00'),
-- Customer 6000: Expecting results to show 3 rows: "1/11 - 1/12"; "1/17 - 1/22"; "1/26 - 1/27"
(6000, 'PRODUCT_A', N'2013-01-01T00:00:00', N'2013-01-04T00:00:00'),
(6000, 'PRODUCT_A', N'2013-01-09T00:00:00', N'2013-01-12T00:00:00'),
(6000, 'PRODUCT_A', N'2013-01-17T00:00:00', N'2013-01-22T00:00:00'),
(6000, 'PRODUCT_A', N'2013-01-26T00:00:00', N'2013-01-31T00:00:00'),
(6000, 'PRODUCT_B', N'2013-01-04T00:00:00', N'2013-01-28T00:00:00'),
(6000, 'PRODUCT_C', N'2013-01-11T00:00:00', N'2013-01-27T00:00:00');
--
--
;
WITH MIN_MAX
AS (SELECT MIN(Eff_Dt) S,
MAX(End_Dt) E
FROM #TMP),
ALL_DATES
AS (SELECT MIN_MAX.S DT
FROM MIN_MAX
UNION ALL
SELECT DATEADD(DAY, 1, ALL_DATES.DT)
FROM ALL_DATES
WHERE ALL_DATES.DT < (SELECT MIN_MAX.E FROM MIN_MAX)),
GROUPED
AS (SELECT Q.CustomerID,
Q.ProductID,
Q.DT,
CASE WHEN MAX(TX.CustomerID) IS NULL THEN 0 ELSE 1 END YES
FROM
(
SELECT *FROM ALL_DATES
CROSS JOIN
(SELECT DISTINCT CustomerID, ProductID FROM #TMP) AS AQ
) AS Q
LEFT JOIN #TMP AS TX
ON TX.CustomerID = Q.CustomerID
AND TX.ProductID = Q.ProductID
AND Q.DT
BETWEEN TX.Eff_Dt AND TX.End_Dt
GROUP BY Q.CustomerID,
Q.ProductID,
Q.DT),
BuildFlags
AS (SELECT G.CustomerID,
G.DT,
ROW_NUMBER() OVER (PARTITION BY G.CustomerID ORDER BY G.DT) RN,
CASE
WHEN WQ.tot =
(
SELECT COUNT(DISTINCT g2.ProductID)
FROM GROUPED AS g2
WHERE g2.CustomerID = G.CustomerID
AND g2.DT = G.DT
AND g2.YES = 1
) THEN 1
ELSE 0
END FLAG
FROM GROUPED AS G
CROSS APPLY
(
SELECT COUNT(DISTINCT E9.ProductID) tot
FROM #TMP AS E9
WHERE E9.CustomerID = G.CustomerID
) AS WQ ),
AddRanks
AS (SELECT *,
BuildFlags.RN - ROW_NUMBER() OVER (PARTITION BY BuildFlags.CustomerID,
BuildFlags.FLAG
ORDER BY BuildFlags.DT
) groupRank
FROM BuildFlags)
SELECT AddRanks.CustomerID,
MIN(AddRanks.DT) AS StartDate,
MAX(AddRanks.DT) AS EndDate
FROM AddRanks
WHERE AddRanks.FLAG = 1
GROUP BY AddRanks.CustomerID,
AddRanks.groupRank
ORDER BY AddRanks.CustomerID,
MIN(AddRanks.DT)
OPTION (MAXRECURSION 0);