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 988875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:42:02+00:00 2026-05-16T05:42:02+00:00

I would like to group query results by consecutive appearances of a column values.

  • 0

I would like to group query results by consecutive appearances of a column values. Let’s say I have a table which lists the winners of a competition for each year as follows:

year    team_name
2000    AAA
2001    CCC
2002    CCC
2003    BBB
2004    AAA
2005    AAA
2006    AAA

I would like a query which outputs:

start_end    total   team_name
2000         1       AAA
2001-2002    2       CCC
2003         1       BBB
2004-2006    3       AAA

I’m not too much worried about the format of “start_end” at long as I have the start and end or range (.e.g. one could use GROUP_CONCAT to produce 2004,2005,2006 instead of 2004-2006 and that would still be OK).

  • 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-16T05:42:03+00:00Added an answer on May 16, 2026 at 5:42 am

    Provided that your table looks like this :

    "id";"year";"team"
    "1";"2000";"AAA"
    "2";"2001";"CCC"
    "3";"2002";"CCC"
    "4";"2003";"BBB"
    "5";"2004";"AAA"
    "6";"2005";"AAA"
    "7";"2006";"AAA"
    

    This query should do the trick :

    SELECT a.year AS start
         , MIN(c.year) AS end
         , MIN(c.year)-a.year+1 AS total
         , CONCAT_WS('-', a.year, IF(a.year = min(c.year), NULL, min(c.year))) as start_end
         , a.team
      FROM 
         ( SELECT x.year, x.team, COUNT(*) id
             FROM results x
             JOIN results y
               ON y.year <= x.year
            GROUP BY x.id
         ) AS a
      LEFT JOIN 
         ( SELECT x.year, x.team, COUNT(*) id 
             FROM results x
             JOIN results y
               ON y.year <= x.year
            GROUP BY x.id
         ) AS b ON a.id = b.id + 1 AND b.team = a.team
      LEFT JOIN  
         ( SELECT x.year, x.team, COUNT(*) id 
             FROM results x
             JOIN results y
               ON y.year <= x.year
            GROUP BY x.id
         ) AS c ON a.id <= c.id AND c.team = a.team
      LEFT JOIN 
         ( SELECT x.year, x.team, COUNT(*) id 
             FROM results x
             JOIN results y
               ON y.year <= x.year
            GROUP BY x.id
         ) AS d ON c.id = d.id - 1 AND d.team = c.team
    WHERE b.id IS NULL AND c.id IS NOT NULL AND d.id IS NULL
    GROUP BY start;
    

    BTW You might find the Common Queries Tree handy to solve these problems (check the answers for “Find previous and next values in a sequence”) :p.

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

Sidebar

Related Questions

I'm using a linq group by query (with two grouping parameters) and would like
I have an HTML document where I would like to semantically group text to
I would like to generate a table (or query result) in this form +---------------------+---------------------+
I would like to simply query table Flights , contains datetime field Arrival ,
I have the following query where I would like to return two result sets.
i have three tables i would like to link in this one query. The
I have inherited search results template in an EE 1.6.8 install and would like
I have SQL update query problem. I would like to be able to update
I have a somewhat simple query in SQL that I would like to convert
I have a query which gets all records ordered by last_name. Now I would

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.