Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6177923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:21:41+00:00 2026-05-24T00:21:41+00:00

week cookie 1 a 1 b 1 c 1 d 2 a 2 b

  • 0
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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:21:42+00:00Added an answer on May 24, 2026 at 12:21 am

    This query uses variables to track adjacent weeks and work out if they are consecutive:

    set @start_week = 2, @week := 0, @conseq := 0, @cookie:='';
    select conseq_weeks, count(*)
    from (
    select 
      cookie,
      if (cookie != @cookie or week != @week + 1, @conseq := 0, @conseq := @conseq + 1) + 1 as conseq_weeks,
      (cookie != @cookie and week <= @start_week) or (cookie = @cookie and week = @week + 1) as conseq,
      @cookie := cookie as lastcookie,
      @week := week as lastweek
    from (select week, cookie from webhist where week >= @start_week order by 2, 1) x
    ) y
    where conseq
    group by 1;
    

    This is for week 2. For another week, change the start_week variable at the top.

    Here’s the test:

    create table webhist(week int, cookie char);
    insert into webhist values (1, 'a'), (1, 'b'), (1, 'c'), (1, 'd'), (2, 'a'), (2, 'b'), (3, 'a'), (3, 'c'), (3, 'd');
    

    Output of above query with where week >= 1:

    +--------------+----------+
    | conseq_weeks | count(*) |
    +--------------+----------+
    |            1 |        4 |
    |            2 |        2 |
    |            3 |        1 |
    +--------------+----------+
    

    Output of above query with where week >= 2:

    +--------------+----------+
    | conseq_weeks | count(*) |
    +--------------+----------+
    |            1 |        2 |
    |            2 |        1 |
    +--------------+----------+
    

    p.s. Good question, but a bit of a ball-breaker

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every visit to my website updates a user's individual hit counter and updates a
week DKK id=radio2 value=75 /> I have bunch of these div entry generated by
this week I'm having problems logging in LinkedIn using ruby mechanize. My code is
This week I have figured out how to modify form elements in the location
For customer service week this year, I have the privileged task of creating a
Earlier this week I ask a question about filtering out duplicate values in sequence
This week, several of our .NET (C#) web applications have been thowing the error
This week I decided to add a new element to a javascript array by
A week ago I would have done this manually: subset dataframe by group to
I take This Week's Revenue and Last Week's Revenue values from the server and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.