块引用复制/粘贴数据:
productNumber Account BuyDate SellDate RelevantYYYYMM
27530 8605148 11/5/15 7:03 PM 11/11/15 2:02 PM 201511
27530 8582063 10/14/15 7:17 PM 10/16/15 1:48 PM 201510
27530 8566748 9/30/15 6:51 PM 10/13/15 2:09 PM 201510
101167 8622268 11/27/15 7:50 PM 11/27/15 8:35 PM 201511
101167 8622267 11/27/15 8:49 AM 11/27/15 11:02 AM 201511
101167 8622265 11/26/15 8:47 AM 11/26/15 11:14 AM 201511
101167 8622272 11/25/15 7:43 PM 11/25/15 8:40 PM 201511
102432 8611407 11/12/15 5:41 PM 11/25/15 11:50 AM 201511
102432 8600664 11/2/15 12:04 AM 11/4/15 12:56 PM 201511
102432 8573179 10/5/15 2:03 PM 10/7/15 3:47 PM 201510
127819 8581822 10/14/15 7:49 PM 10/18/15 10:46 AM 201510
127819 8572308 10/2/15 4:38 PM 10/4/15 2:28 PM 201510
127819 8571220 10/2/15 9:34 AM 10/2/15 4:25 PM 201510
141105 8612529 11/16/15 7:45 AM 11/16/15 3:06 PM 201511
141105 8612460 11/14/15 10:04 AM 11/14/15 12:48 PM 201511
141105 8608738 11/10/15 12:53 PM 11/10/15 3:19 PM 201511
141105 8603007 11/3/15 5:28 PM 11/9/15 3:54 PM 201511
141105 8595018 10/28/15 7:23 PM 11/2/15 2:09 PM 201511
141105 8562238 10/22/15 8:40 AM 10/27/15 5:11 PM 201510
我需要按 RelevantYYYYMM 计算 ProductNumber 组(201512 表示 2015 年 12 月)。但是,逻辑应该是:
- ProductNumber 及其最近的 BuyDate 应在该 RelevantYYYYMM 内。
- 与 ProductNumber 的最新 BuyDate 关联的先前 BuyDate 应在 30 天范围内。示例:产品 27530 的最近购买日期是 2015 年 11 月 5 日。产品的先前购买日期为 10/14/15,即 11.5.15 后的 30 天内。
- 第 3 次购买日期应在第 2 次购买日期后的 30 天内。示例:ProductNumber 27530 的第二个 BuyDate 是 10/14/15,第三个 BuyDate 是 9/30/15,在 10/14/15 的 30 天范围内。
- 应对与该编号关联的每个 ProductNumber 和 RelevantYYYYMM 进行此验证。示例:对于 RelevantYYYYMM 201510(2015 年 10 月),ProductNumber 27530 不应出现在我的计数中,因为未满足 Stpes 1-3。说明n:201510(2015 年 10 月)中 ProductNumber 27530 的最近日期是 2015 年 10 月 14 日。以前的 BuyDate 是 2015 年 9 月 30 日,即 2015 年 10 月 14 日的 30 天范围内。但是,该 ProductNumber 在 2015 年 9 月 30 日之前没有另一个在 30 天范围内的 BuyDate。它无法验证步骤 3,因此它被从计数中消除。
代码:
--get all products and assign YYYYMM
drop table #firstbuy
declare @StartDate as date = '01/01/2015'
declare @EndDate as date = '12/31/2015'
select ProductNumber, Account, BuyDate, SellDate,
CAST(DATEPART(yyyy, BuyDate) AS varchar(4))+
CAST(REPLICATE('0', 2-LEN(MONTH(BuyDate))) AS varchar(2)) +
CAST(DATEPART(MONTH, BuyDate) AS varchar(2)) AS RelevantYYYYMM
into #firstbuy
From products V
WHERE
V.BuyDate BETWEEN @StartDate AND @EndDate
--select * from #firstbuy
--Rank by BuyDate desc. Use ProductNumber and RelevantYYYYMM for partition by
drop table #firstbuypart2
select *
into #firstbuypart2
from (
select ProductNumber, Account, BuyDate, SellDate, RelevantYYYYMM,
ROW_NUMBER() OVER (PARTITION BY ProductNumber, RelevantYYYYMM ORDER BY BuyDate DESC) AS getcount1
from #firstbuy
) a
--select * from #firstbuypart2
--Grab records that has count = 1. This will give you most recent BuyDate per RelevantYYYYMM
drop table #firstbuypart3
select ProductNumber, BuyDate, SellDate, RelevantYYYYMM
into #firstbuypart3
from #firstbuypart2
where getcount1 = 1
Group BY ProductNumber, RelevantYYYYMM, BuyDate, SellDate,
order by ProductNumber
--select * from #firstbuypart3
--Not sure what logic to use to get only one specific row per ProductNumber and RelevantYYYYMM
drop table #firstbuypart4
select b.*, a.BuyDate as BuyDateA, a.SellDate as SellDateA,
CAST(DATEPART(yyyy, a.BuyDateA) AS varchar(4))+
CAST(REPLICATE('0', 2-LEN(MONTH(a.BuyDateA))) AS varchar(2)) +
CAST(DATEPART(MONTH, a.BuyDateA) AS varchar(2)) AS RelevantYYYYMM_A
into #firstdischargepart4
From products a
JOIN #firstbuypart3 b on a.ProductNumber = b.ProductNumber
WHERE
DATEDIFF(DAY,b.BuyDate,a.BuyDate) BETWEEN 0 AND 30
--select * from #firstbuypart4