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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:09:54+00:00 2026-05-27T10:09:54+00:00

If I have a SQL table that looks like this below how can I

  • 0

If I have a SQL table that looks like this below how can I calculate the current winning or losing streak (and having the winning or losing streak grouped/reset by season). I want to update the table and fill in the streak for every record.

So for #1 the streak would be “-1”, #2 would be “1”, #3 would be “2” but once we got down to #7 it would be reset to “1” again. (+1 means “Won 1 game” and -1 means “Lost 1 game”, etc.)

ID    team    date         Result    season     streak
1     76ers   2000-01-01   Loss      2000       Null
2     76ers   2000-01-05   Win       2000       Null
3     76ers   2000-01-08   Win       2000       Null
4     Lakers  2000-01-03   Loss      2000       Null
5     Lakers  2000-01-07   Loss      2000       Null
6     Lakers  2000-01-01   Win       2000       Null

7     76ers   2002-03-01   Win       2001       Null
8     76ers   2002-03-05   Win       2001       Null
9     76ers   2002-03-08   Loss      2001       Null
10    Lakers  2002-03-03   Loss      2001       Null
11    Lakers  2002-03-07   Loss      2001       Null
12    Lakers  2002-03-01   Win       2001       Null
  • 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-27T10:09:54+00:00Added an answer on May 27, 2026 at 10:09 am

    For each game, count games with the same result that came before it, such that there is no game with the opposite result in between. Store the results in a temporary table:

    CREATE TEMPORARY TABLE STREAK_TABLE
    SELECT
        ID,
        (
            SELECT 1 + COUNT(*)                 -- Earlier games with the same result, team and season.
            FROM YOUR_TABLE T2
            WHERE
                T1.Result = T2.Result
                AND T1.team = T2.team
                AND T1.season = T2.season
                AND T1.date > T2.date
                AND NOT EXISTS (
                    SELECT *                    -- The games in between, with the same team and season but opposite result.
                    FROM YOUR_TABLE T3
                    WHERE
                        T2.Result <> T3.Result
                        AND T1.team = T3.team
                        AND T1.season = T3.season
                        AND T3.date BETWEEN T2.date AND T1.date
                )
        ) S
    FROM YOUR_TABLE T1
    

    Then, update the original table (and negate the losing streaks in the process):

    UPDATE YOUR_TABLE
    SET streak = (
        SELECT CASE Result WHEN 'Win' THEN S ELSE -S END
        FROM STREAK_TABLE
        WHERE STREAK_TABLE.ID = YOUR_TABLE.ID
    )
    

    Finally, clean-up the temporary table:

    DROP TABLE STREAK_TABLE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi there I have an SQL table that looks like this: CREATE TABLE IF
I have a SQL table set that looks like this create table foo (
I'm using SQL Server 2008. I have a database table that looks like this
I have an SQL table called trainings that looks like this: +-----+-----------+--------+------------+------------+------------+-------+ | Id
I have a SQL statement that looks like: SELECT [Phone] FROM [Table] WHERE (
I have a table that looks something like this: CREATE TABLE student_results(id integer, name
I have a table that looks like this: ---------------------- | DateTime | Value |
for SQL Server 2008 R2 I have a resultset that looks like this (note
I have a table that looks like this:- TABLEA SERIAL SKU GRADE ID HA501
Let's say I have a table that looks like this: Id CheckboxId Selected DateCreated

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.