Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SELECT SUM(numbering) as numbering
- FROM
- (
- SELECT
- CASE WHEN row_num >= 3 and (lead(row_num) over(order by id) = 1 or LEAD(row_num) over(order by id) is null) then 1 else 0 end as numbering
- FROM
- (
- select id, val,
- row_number() over (partition by grp order by id) as row_num
- from (select t.id,t.VAL,
- (ROW_NUMBER() over (order by id) -
- ROW_NUMBER() over (partition by val order by id)
- ) as grp
- FROM dbo.testing t
- ) t
- ) as t2
- WHERE VAL = 1
- ) as t3
- CREATE TABLE DBO.testing(id char(3), VAL int)
- insert into dbo.testing(id , val)
- VALUES
- (
- 'A01', 1),
- ('A02', 1),
- ('A03', 1),
- ('A04', 0),
- ('A10', 1),
- ('A11', 1),
- ('A12', 0),
- ('A13', 1),
- ('A14', 1),
- ('A15', 1),
- ('A16', 1)
- numbering
- 2
Add Comment
Please, Sign In to add comment