i have got the set of data as follow
name date
x 2014-01-01
x 2014-01-02
y 2014-01-03
x 2014-01-04
and i'm trying to get this result
name date row_num
x 2014-01-01 1
x 2014-01-02 2
y 2014-01-03 1
x 2014-01-04 1
i have tried to run this query
select name,
date,
row_number () over (partition by name order by date) as row_num
from myTBL
but unfortunately i get this result
name date row_num
x 2014-01-01 1
x 2014-01-02 2
y 2014-01-03 1
x 2014-01-04 3
please help.
You need to identify the groups of
names
that occur together. You can do this with a difference of row numbers. Then, use thegrp
for partitioning therow_number()
:For your sample data:
This should give you an idea of how it works.