Home t-sql: dense rank that is increased at every occurence of a flag
Reply: 1

t-sql: dense rank that is increased at every occurence of a flag

Ingvar
1#
Ingvar Published in 2017-12-07 10:25:23Z

I have a table that has Parent Rows and Children Rows. The table consists of an increasing sequence, a row indicating whether it is a child or a parent and a code. I need to create ranking (an increasing number) that is increased only at every occurrence of IsParent column (values:0,1).

This is the table that I have: original table

This is the desirable result table: result table

Larnu
2#
Larnu Reply to 2017-12-07 10:30:38Z

DDL and Sample data would be great, however, looks like you want to use a Window Function. This isn't tested with your data, but this should give you the expected output.

SUM(IsParent) OVER (ORDER BY [Row Number]
                    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.366934 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO