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

  • SEARCH
  • Home
  • 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 6770285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:17:39+00:00 2026-05-26T15:17:39+00:00

id || week_id || user_id || catch_id(0,1,2) 1 || 2 || 6 || 0

  • 0
id  || week_id || user_id || catch_id(0,1,2)
1   || 2       || 6       || 0
1   || 3       || 6       || 1
1   || 4       || 6       || 1
1   || 5       || 6       || 1
1   || 6       || 6       || 0
1   || 7       || 6       || 0
1   || 8       || 6       || 2
1   || 9       || 6       || 0
1   || 10      || 6       || 0
1   || 11      || 6       || 1

I need find out the max consecutive week the catch = 1 and max consecutive week the catch = 0 for each user (find all). I hope i make myself clear.

In the above table

max consecutive catch = 1 for user 6 is 3(weeks 3,4,5)

max consecutive weeks catch = 0 for user 6 is 2(week 6,7 and/or week 9,10)

How do i go about. Can i do this in purely sql. A php solution is also welcome

  • 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-26T15:17:39+00:00Added an answer on May 26, 2026 at 3:17 pm

    This should work for a SQL solution. Though it will only ever give you one week_id for the catch_id in question. I don’t know what your table is called so I’ve called it consecutive in the answer below:

    drop table if exists consecutive;
    
    create table consecutive
    (id int,week_id int,user_id int,catch_id int);
    
    insert into consecutive values (1,2,6,0);
    insert into consecutive values (1,3,6,1);
    insert into consecutive values (1,4,6,1);
    insert into consecutive values (1,5,6,1);
    insert into consecutive values (1,6,6,0);
    insert into consecutive values (1,7,6,0);
    insert into consecutive values (1,8,6,2);
    insert into consecutive values (1,9,6,0);
    insert into consecutive values (1,10,6,0);
    insert into consecutive values (1,11,6,1);
    
    select w,count(*) as max_consecutive_weeks
    from
    (
    select
    case when @cur_catch_id != catch_id then @cur_week_id := week_id else @cur_week_id end as w,
    @cur_catch_id := catch_id as catchChange,
    c.*
    from consecutive c
    cross join (select @cur_catch_id := -1,@cur_week_id := -1) t
    where user_id = 6
    order by week_id asc
    ) t
    where catch_id = 1
    group by w
    order by max_consecutive_weeks desc,w asc
    limit 1;
    

    You can use the same query to get max consecutive week_ids with catch_id = 0 by changing where catch_id = 1 to where catch_id = 0.

    Good luck!

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

Sidebar

Related Questions

This week I have figured out how to modify form elements in the location
Earlier this week I ask a question about filtering out duplicate values in sequence
I've been trying to figure out for a week or so, how I can
I am trying to sort articles by Today, This Week, This Month , All
I've a voting system which is actually simple. vote_id, user_id, article_id I need now
I need a query that will have the date, a user's name, and one
I have 7 summary tables one for each day of the week. txn_summary_monday...txn_summary_tuesday and
I have a error that I can't find the solution. When I run: Week.find(1).results.sum('box')
I need to generate a report as follow: week-ending date | number of active
I'm new to C# (started last week) so be cool with me ;). I'd

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.