Saturday, 15 January 2011

sql - Need help to aggregate/count a query -


I have a table:

  214111 12-Jun-13 06:17: 11 Going forward 143249 57 0 57 0 214111 12-Jun-16: 35 Proceeding project 143249 55 0 55 0 214111 12-Jun-13 06:19:14 Moving speed 143249 71 71 71 214111 12-Jun-13 06:21 : Moving Project 143249 70 65 5 1 214111 12-Jun-13 06:23:14 Moving Project 143249 70 0 70 0 214111 12-Jun-13 06:32:10 Moving Project 143249 40 0 ​​40 0 ​​214111 12-Jun-13 06:55:35 Going forward, PRID 143249 63 55 8 1 214111 12-Jun-13 06:56:22 Proceeding the project 143249 60 55 5 1 214111 12-Jun-07 07:02:43 PMID 143307 27 Running 0 27 0 214111 12-Jun-13 07:04:15 Going forward 1432307 32 32 32 321 214111 12-Jun-07 07:04:25 Proceeding the project 143307 22 22 22 211111 12-Jun-07 07:08:45 Moving PRID 143307 34 34 34 1   

I want to count the number of consecutive rows 1. The counter will be reset if it is 0.

  214111 12-JUN-13 06:17:11 Going forward 143249 57 0 57 0 0 214111 12-Jun-06 06:35 Moving forward 143249 55 0 55 0 0 214111 12-Jun Moving Speed ​​143249 71 71 71 0 0 214111 12-Jun-13 06:21:14 Further Increasing 143249 70 65 5 1 1 214111 12-Jun-13 06:23:14 Going forward, PRID 143249 70 0 70 0 0 214111 12-Jun-13 06:32:10 Proceeding project 143249 40 0 ​​40 0 ​​214111 12-Jun-13 06:55:35 PRI 143249 63 55 8 1 Running 1 214111 12-Jun-13 06:56:22 Going forward, PRID 143249 60 55 5 1 2 214111 12-Jun-07 07:02:43 Proceeding the project 143307 27 0 27 0 214111 12-Jun-17: 04: 15 Walking PRID 143307 32 0 32 1 1 214111 12-Jun-07 07:04:25 Forward B Projects 143307 22 22 222 2 214111 12-Jun-07 07:08:45 Moving Project 143307 34 34 34 3 3 3 4 5 6 7 / <>  

Need Help ... < / Div>

It is a bit tricky to recognize that serial number lag () - 0 using a 0 (Or zero) starts after. Then the cumulative sum of those values ​​identifies the sequence starting with cumulative zodiac 1 after 0. Then get a cumulative amount and some arguments to get the final value:

  select t. *, (When the value = 0 then greater than 0 and SUM (IND)) (as opposed to the GRP order by DTD) as the counter (selection T. *, SUM (IND) over (order by DT) From the form of GRP (Selection T., (when the case = 1 and the lag (val) over (order by DT = 0) then 1 and 0 in the form of T) indirectly as T)    

No comments:

Post a Comment