我有这样的结果集:
结果
L
w ^
w ^
w ^
→
→
w ^
w ^
w ^
我想计算W在SQL Server中连续三次出现的时间。请帮忙!
答案 0 :(得分:2)
您想要的是SQL Server中的LEAD / LAG功能。
这是一个工作样本,假设1 =胜利,0 =亏损。
CREATE TABLE WINLOSS (
WINLOSSID INT NOT NULL IDENTITY PRIMARY KEY,
WIN BIT NOT NULL
);
INSERT INTO WINLOSS VALUES (1),(0),(1),(1),(1),(0),(1),(0),(1),(1),(1);
WITH TMP AS (
SELECT
WINLOSSID,
WIN,
LAG(WIN, 1) OVER (ORDER BY WINLOSSID) LAG1,
LAG(WIN, 2) OVER (ORDER BY WINLOSSID) LAG2
FROM WINLOSS
)
SELECT COUNT(1)
FROM TMP
WHERE WIN = 1 AND LAG1 = 1 AND LAG2 = 1