SQL Server How many times 1 is in continues three times in a column? [closed]

v440hwme  于 2023-06-04  发布在  其他
关注(0)|答案(1)|浏览(167)

Closed. This question needs details or clarity . It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post .

Closed 14 hours ago.
Improve this question

I have a table Tbl_A there is only one column Id . There are multiple numbers like as below mention. I want to get count that how many times 1 is occurring in continues three times.

Data :-

id
1
1
2
2
2
1
1
1
3
1
1
1

create table Tbl_A (id int);

insert into Tbl_A
select 1 Union All
select 1 Union All
select 2 Union All
select 2 Union All
select 2 Union All
select 1 Union All
select 1 Union All
select 1 Union All
select 3 Union All
select 1 Union All
select 1 Union All
select 1 ;

Expected Output :- 2 (Because 1 is only 2 times occurring in continues three times )

I have tried with Lag, Lead also with Rank but no luck.

g6ll5ycj

g6ll5ycj1#

As much deterministic sorting it can get for TBL_A to order by the physical location of a row.

WITH DER AS(SELECT A.ID,SUM(CASE WHEN A.ID=1 THEN 0 ELSE 1 END)OVER(ORDER BY F.FILE_ID,F.PAGE_ID,F.SLOT_ID)SM
FROM TBL_A A WITH(INDEX(0))CROSS APPLY sys.fn_PhysLocCracker(%%physloc%%)F), 
ID13 AS(SELECT SM FROM DER WHERE ID=1 GROUP BY SM HAVING COUNT(1)=3)
SELECT COUNT(1)[EXPECTED OUTPUT]
FROM ID13

相关问题