week cookie
1 a
1 b
1 c
1 d
2 a
2 b
3 a
3 c
3 d
This table represent someone visits a website in a particular week. Each cookie represents an individual person. Each entry represent someone visit this site in a particular week. For example, the last entry means ‘d’ come to the site in week 3.
I want to find out how many (same) people keep coming back in the following week, when given a start week to look at.
For example, if I look at week 1. I will get result like:
1 | 4
2 | 2
3 | 1
Because 4 user came in week 1. Only 2 of them (a,b) came back in week 2. Only 1 (a) of them came in all of these 3 weeks.
How can I do a select query to find out? The table will be big: there might be 100 weeks, so I want to find the right way to do it.
This query uses variables to track adjacent weeks and work out if they are consecutive:
This is for week 2. For another week, change the
start_weekvariable at the top.Here’s the test:
Output of above query with
where week >= 1:Output of above query with
where week >= 2:p.s. Good question, but a bit of a ball-breaker