Guest User

Untitled

a guest
Jul 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. DECLARE @Data TABLE
  2. (
  3. Sequence TINYINT NOT NULL PRIMARY KEY,
  4. Subset CHAR(1) NOT NULL
  5. )
  6. INSERT INTO @Data (Sequence, Subset) VALUES
  7. (1, 'A'),
  8. (2, 'A'),
  9. (3, 'A'),
  10. (4, 'B'), -- New subset
  11. (5, 'B'),
  12. (6, 'A') -- New subset
  13.  
  14. SELECT
  15. Sequence, Subset,
  16. ROW_NUMBER() OVER (PARTITION BY Subset ORDER BY Sequence) AS SeqWithinGroup
  17. FROM
  18. @Data
  19.  
  20. Sequence Subset Expected Actual
  21. -------- ------ -------- -----
  22. 1 A 1 1
  23. 2 A 2 2
  24. 3 A 3 3
  25. 4 B 1 1
  26. 5 B 2 2
  27. 6 A *1* *4*
  28.  
  29. SELECT
  30. Sequence, Subset,
  31. CASE WHEN Sequence = 1 OR Subset <> LAG(Subset, 1) OVER (ORDER BY Sequence)
  32. THEN 'New subset'
  33. ELSE 'Continuation'
  34. END
  35. FROM
  36. @Data
Add Comment
Please, Sign In to add comment