oracle - sql for counting null in between and inserting number -


i have table column c1.i need c2 this:

    c1        c2 (somevalue)   3 (somevalue)   3    null       3  (somevalue)   2    null       2 (somevalue)   3    null       3    null       3 (somevalue)   2    null       2 (somevalue)   1 

logic:count rows not null until null found followed not null value, including nulls, without following value. start counting again when null crossed until next null found.

the following answer original question op wanted count null values.

    c1        c2    null       3    null       3  (somevalue)   3    null       4    null       4    null       4 (somevalue)   4    null       2 (somevalue)   2 

assuming columns ordered id column, might want:

select id, c1, x, y,     count(1) on (partition y) z from(     select id, c1, x,         lag(x,1,x) on (order id) y     (         select              id,              c1,              count(c1) on (order id) x          test     ) ) order id; 

of course can remove superfluous columns outermost select statement.

there sql fiddle it.

an answer current version can obtained replacing c1 in answer decode(c1,null, 'x', null) , possibly minor adjustments border cases.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -