pluck
pluck

Reputation: 97

Count number of rows before date per id

I'm not sure how else to explain it other than the title. I'm basically trying to get the number of rows per id before the date on that specific row. I've tried a bunch of things and scoured the internet to no avail. Please help!

Before

id  date  
1   3/3/2015  
2   3/27/2015  
2   4/15/2015  
2   5/1/2015  
3   3/7/2015  
3   5/17/2015  
3   7/9/2015  
3   7/19/2015

After

id  date        count
1   3/3/2015    0
2   3/27/2015   0
2   4/15/2015   1
2   5/1/2015    2
3   3/7/2015    0
3   5/17/2015   1
3   7/9/2015    2
3   7/19/2015   3

Upvotes: 0

Views: 141

Answers (1)

shawnt00
shawnt00

Reputation: 17915

-1 + row_number() over (partition by id order by date)

Upvotes: 3

Related Questions