块引用 复制/粘贴数据:
submit
我需要通过RelevantYYYYMM获得ProductNumber组的计数(201512表示2015年12月)。但是,逻辑应该是:
代码:
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
答案 0 :(得分:1)
我认为这可能是你需要的:
ATTENTION_1:您不应该使用特定于文化的日期时间格式!看这里:https://stackoverflow.com/a/34275965/5089204
注意_2:注意DateTime!如果有时间,BETWEEN @StartDate AND @EndDate
将不会包含来自@EndDate的BuyDates!
SET LANGUAGE ENGLISH;
DECLARE @tbl TABLE(productNumber INT,Account INT,BuyDate DATETIME,SellDate DATETIME);
INSERT INTO @tbl VALUES
(27530,8605148,'11/5/15 7:03 PM','11/11/15 2:02 PM')
,(27530,8582063,'10/14/15 7:17 PM','10/16/15 1:48 PM')
,(27530,8566748,'9/30/15 6:51 PM','10/13/15 2:09 PM')
,(101167,8622268,'11/27/15 7:50 PM','11/27/15 8:35 PM')
,(101167,8622267,'11/27/15 8:49 AM','11/27/15 11:02 AM')
,(101167,8622265,'11/26/15 8:47 AM','11/26/15 11:14 AM')
,(101167,8622272,'11/25/15 7:43 PM','11/25/15 8:40 PM')
,(102432,8611407,'11/12/15 5:41 PM','11/25/15 11:50 AM')
,(102432,8600664,'11/2/15 12:04 AM','11/4/15 12:56 PM')
,(102432,8573179,'10/5/15 2:03 PM','10/7/15 3:47 PM')
,(127819,8581822,'10/14/15 7:49 PM','10/18/15 10:46 AM')
,(127819,8572308,'10/2/15 4:38 PM','10/4/15 2:28 PM')
,(127819,8571220,'10/2/15 9:34 AM','10/2/15 4:25 PM')
,(141105,8612529,'11/16/15 7:45 AM','11/16/15 3:06 PM')
,(141105,8612460,'11/14/15 10:04 AM','11/14/15 12:48 PM')
,(141105,8608738,'11/10/15 12:53 PM','11/10/15 3:19 PM')
,(141105,8603007,'11/3/15 5:28 PM','11/9/15 3:54 PM')
,(141105,8595018,'10/28/15 7:23 PM','11/2/15 2:09 PM')
,(141105,8562238,'10/22/15 8:40 AM','10/27/15 5:11 PM');
declare @StartDate as date = '01/01/2015';
declare @EndDate as date = '12/31/2015';
WITH Extended AS
(
SELECT ProductNumber
,Account
,BuyDate
,SellDate
,CAST(YEAR(BuyDate) AS VARCHAR(4))+ REPLACE(STR(MONTH(BuyDate),2),' ','0') AS RelevantYYYYMM
,ROW_NUMBER() OVER(PARTITION BY ProductNumber ORDER BY productNumber,BuyDate DESC) AS RowInx
,COUNT(BuyDate) OVER(PARTITION BY ProductNumber) AS CountBuyDates
FROM @tbl
WHERE BuyDate BETWEEN @StartDate AND @EndDate --ATTENTION!!! Be aware of DateTime! this will NOT include BuyDates from @EndDate, if there is a time!
)
,DayDiffs AS
(
SELECT *
,DATEDIFF(DAY,ext.BuyDate
,(SELECT innerExt.BuyDate FROM Extended AS innerExt WHERE innerExt.productNumber=ext.productNumber AND innerExt.RowInx=ext.RowInx-1)) AS IntervalRecentToSecond
,DATEDIFF(DAY,(SELECT innerExt.BuyDate FROM Extended AS innerExt WHERE innerExt.productNumber=ext.productNumber AND innerExt.RowInx=ext.RowInx-1)
,(SELECT innerExt.BuyDate FROM Extended AS innerExt WHERE innerExt.productNumber=ext.productNumber AND innerExt.RowInx=ext.RowInx-2)) AS IntervalSecondToThird
FROM Extended AS ext
WHERE ext.CountBuyDates>=3 --min three
AND ext.RowInx=3 --this is the relevant row
)
SELECT productNumber
,Account
,RelevantYYYYMM AS Relev
,IntervalRecentToSecond AS Diff1To2
,IntervalSecondToThird AS Diff2To3
FROM DayDiffs
结果将是:
productNumber Account Relev Diff1To2 Diff2To3
27530 8566748 201509 14 22
101167 8622265 201511 1 0
102432 8573179 201510 28 10
127819 8571220 201510 0 12
141105 8608738 201511 4 2
我必须承认,我真的不明白,你的" RelevantYYYYMM"应该管用。但是我认为,你可以在自己中摆弄这个......如果不是,那就回过头来做一个简短的解释。