一个真正的头脑风暴就在这里。
我想标记de X1之间的过渡。因此,从A-> B,B-> A和B-> C,C-> B过渡。
这是为了将我的数据集减少为仅具有过渡的记录。
最终目标是计算从最后一次出现X1 A到第一次出现X1 B的日期之间的天数。
例如:
ID1->从A到B的过渡,日期为2018-01-20和2018-02-01。那么答案必须是x天数之间。
OR
示例2:ID1->从A过渡到B,日期为2018-02-16和2018-03-01。然后答案必须是x天数之间。
ID X1 Date
1 A 2018-01-01
1 A 2018-01-20
1 B 2018-02-01
1 A 2018-02-15
1 A 2018-02-16
1 B 2018-03-01
2 B 2018-01-01
2 C 2018-03-05
2 C 2018-03-06
2 C 2018-03-08
2 B 2018-03-20
我已经有了这个循环,但是这个循环花费的时间太长(每条记录0.3秒):
DECLARE @Loop INT
DECLARE @MAXROWS INT
SET @Loop = 1
SET @MAXROWS = (SELECT COUNT (*) FROM Y_Table)
WHILE (@Loop <= @MAXROWS)
BEGIN
UPDATE Y_Table
SET Y_Table.Test_MIN = (CASE WHEN
(SELECT Y_Table.ID FROM Y_Table WHERE LIFE_CYCLE = @Loop) =
(SELECT Y_Table.ID FROM Y_Table WHERE LIFE_CYCLE = @Loop + 1) AND
(SELECT Y_Table.Date FROM Y_Table WHERE LIFE_CYCLE = @Loop) <
(SELECT Y_Table.Date FROM Y_Table WHERE LIFE_CYCLE = @Loop + 1) AND
(SELECT Y_Table.X1 FROM Y_Table WHERE LIFE_CYCLE = @Loop) <>
(SELECT Y_Table.X1 FROM Y_Table WHERE LIFE_CYCLE = @Loop + 1)
THEN '1' ELSE '0' END)
FROM Y_Table
WHERE
LIFE_CYCLE = @Loop
UPDATE Y_Table
SET Y_Table.Test_MAX = (CASE WHEN
(SELECT Y_Table.ID FROM Y_Table WHERE LIFE_CYCLE = @Loop) =
(SELECT Y_Table.ID FROM Y_Table WHERE LIFE_CYCLE = @Loop + 1) AND
(SELECT Y_Table.Date FROM Y_Table WHERE LIFE_CYCLE = @Loop) <
(SELECT Y_Table.Date FROM Y_Table WHERE LIFE_CYCLE = @Loop + 1) AND
(SELECT Y_Table.X1 FROM Y_Table WHERE LIFE_CYCLE = @Loop) <>
(SELECT Y_Table.X1 FROM Y_Table WHERE LIFE_CYCLE = @Loop + 1)
THEN '1' ELSE '0' END)
FROM Y_Table
WHERE
LIFE_CYCLE = @Loop + 1
PRINT @Loop
SET @Loop = @Loop + 1
END
如何解决这个问题?
答案 0 :(得分:3)
类似这样的东西
DECLARE @data table (ID int, X1 char(1), myDate date)
INSERT @data VALUES (1, 'A', '2018-01-01'),
(1, 'A', '2018-01-20'),
(1, 'B', '2018-02-01'),
(1, 'A', '2018-02-15'),
(1, 'A', '2018-02-16'),
(1, 'B', '2018-03-01'),
(2, 'B', '2018-01-01'),
(2, 'C', '2018-03-05'),
(2, 'C', '2018-03-06'),
(2, 'C', '2018-03-08'),
(2, 'B', '2018-03-20');
WITH GetNextRows AS (
SELECT
*,
NextX1 = LEAD (X1) OVER (PARTITION BY ID ORDER BY myDate),
NextDate = LEAD (myDate) OVER (PARTITION BY ID ORDER BY myDate)
FROM
@data
)
SELECT
*,
Transation = CONCAT(X1, ' -> ', NextX1),
DaysGap = DATEDIFF(DAY, myDate, NextDate)
FROM
GetNextRows
WHERE
NextX1 <> X1;
ID X1 myDate NextX1 NextDate Transation DaysGap
1 A 2018-01-20 B 2018-02-01 A -> B 12
1 B 2018-02-01 A 2018-02-15 B -> A 14
1 A 2018-02-16 B 2018-03-01 A -> B 13
2 B 2018-01-01 C 2018-03-05 B -> C 63
2 C 2018-03-08 B 2018-03-20 C -> B 12
答案 1 :(得分:1)
您可以使用LAG
来获取先前的X1
和Date
值。如果当前的X1
值与先前的值不同,则您可以进行转换,并可以按天计算差异。
DECLARE @DataSource TABLE
(
[ID] SMALLINT
,[X1] CHAR(1)
,[Date] DATE
);
INSERT INTO @DataSource ([ID], [X1], [Date])
VALUES ('1', 'A', '2018-01-01')
,('1', 'A', '2018-01-20')
,('1', 'B', '2018-02-01')
,('1', 'A', '2018-02-15')
,('1', 'A', '2018-02-16')
,('1', 'B', '2018-03-01')
,('2', 'B', '2018-01-01')
,('2', 'C', '2018-03-05')
,('2', 'C', '2018-03-06')
,('2', 'C', '2018-03-08')
,('2', 'B', '2018-03-20');
SELECT *
,IIF
(
[X1] <> LAG([X1], 1, NULL) OVER(PARTITION BY [ID] ORDER BY [Date] ASC)
,DATEDIFF(DAY, LAG([Date], 1, NULL) OVER(PARTITION BY [ID] ORDER BY [Date] ASC), [Date])
,NULL
) AS [TransitionInDays]
FROM @DataSource;
请注意,您需要SQL Server 2012+才能使用LAG
。让我知道是否不是这种情况。