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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:55:22+00:00 2026-06-14T10:55:22+00:00

I have a database that has a table called matchstats which includes a column

  • 0

I have a database that has a table called matchstats which includes a column called time and it is updated each time an action takes place. I also have a column called groundstatsid which when it is not null means the action took place on the ground as opposed to standing. Finally I have a column called Round.

Example:

Time | groundstatsid | Round

 1   | NULL          |  1
 8   | NULL          |  1
 15  | NULL          |  1
 18  | 1             |  1
 20  | 1             |  1
 22  | NULL          |  1
 30  | NULL          |  1
 1   | NULL          |  2

To get the full time standing I would basically want the query to take the first time (1) and store that, then look at groundstatsid until it sees a NON NULL value and take the time at that position, subtract by the earlier number stored to get the time in standup (17). Then it would continue to look for where groundstatsid IS NULL. Once it finds that value it should do the same process of looking until it finds a NON NULL value in groundstatsid or a new round, in which case it will start the whole process again.

Once it has gone through an entire match I would want it to Sum the results.

I would expect the query of the example to return 25.

  • 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-06-14T10:55:23+00:00Added an answer on June 14, 2026 at 10:55 am

    I would boil this problem down one where you consider pairs of rows, sorted by time within each round. PostgreSQL can do this in one pass — no JOINs, no PL/pgSQL — using window functions:

    SELECT
      round,
      first_value(time) OVER pair AS first_time,
      last_value(time) OVER pair AS last_time,
      first_value(groundstatsid IS NULL) OVER pair AS first_is_standing,
      last_value(groundstatsid IS NULL) OVER pair AS last_is_standing
    FROM matchstats
    WINDOW pair AS (PARTITION BY round ORDER BY time ROWS 1 PRECEDING);
    

    This tells PostgreSQL to read the rows from the table (presumably constrained by WHERE fightid=? or something), but to consider each round separately for windowing operations. Window functions like first_value and last_value can access the “window”, which I specified to be ORDER BY time ROWS 1 PRECEDING, meaning the window contains both the current row and the one immediately preceding it in time (if any). Thus, window functions let us directly output values for both the current row and its predecessor.

    For the data you provided, this query yields:

     round | first_time | last_time | first_is_standing | last_is_standing 
    -------+------------+-----------+-------------------+------------------
         1 |          1 |         1 | t                 | t
         1 |          1 |         8 | t                 | t
         1 |          8 |        15 | t                 | t
         1 |         15 |        18 | t                 | f
         1 |         18 |        20 | f                 | f
         1 |         20 |        22 | f                 | t
         1 |         22 |        30 | t                 | t
         2 |          1 |         1 | t                 | t
    

    Looking at these results helped me decide what to do next. Based on my understanding of your logic, I conclude that the person should be regarded as standing from time 1..1, 1..8, 8..15, 15..18, not standing from 18..20, not standing from 20..22, and is standing again from 22..30. In other words, we want to sum the difference between first_time and last_time where first_is_standing is true. Turning that back into SQL:

    SELECT round, SUM(last_time - first_time) AS total_time_standing
    FROM (
      SELECT
        round,
        first_value(time) OVER pair AS first_time,
        last_value(time) OVER pair AS last_time,
        first_value(groundstatsid IS NULL) OVER pair AS first_is_standing,
        last_value(groundstatsid IS NULL) OVER pair AS last_is_standing
      FROM matchstats
      WINDOW pair AS (PARTITION BY round ORDER BY time ROWS 1 PRECEDING)
    ) pairs
    WHERE first_is_standing
    GROUP BY round;
     round | total_time_standing 
    -------+---------------------
         1 |                  25
         2 |                   0
    

    You could also get other values from this same inner query, like the total time or the number of falls by using SUM(CASE WHEN ...) to count independent conditions:

    SELECT
      round,
      SUM(CASE WHEN first_is_standing THEN last_time - first_time ELSE 0 END) AS total_time_standing,
      SUM(CASE WHEN first_is_standing AND NOT last_is_standing THEN 1 ELSE 0 END) AS falls,
      SUM(last_time - first_time) AS total_time
    FROM (
      SELECT
        round,
        first_value(time) OVER pair AS first_time,
        last_value(time) OVER pair AS last_time,
        first_value(groundstatsid IS NULL) OVER pair AS first_is_standing,
        last_value(groundstatsid IS NULL) OVER pair AS last_is_standing
      FROM matchstats
      WINDOW pair AS (PARTITION BY round ORDER BY time ROWS 1 PRECEDING)
    ) pairs
    GROUP BY round;
    
     round | total_time_standing | falls | total_time 
    -------+---------------------+-------+------------
         1 |                  25 |     1 |         29
         2 |                   0 |     0 |          0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ms access database that has one table for each photo album
I have a database table called users This table has two columns (that are
We're designing a database that has a simple table called Person. Each person can
Given a Database that has a table Called [Bid Transactions], which contains records of
I have a database table called posts that has 'id, title, slug and content'
I have a database that has a table called products, and into the table
I have a database table called item that has a self-referencing field called itemParentID.
I have a database that has a table email_eml that stores 3 attributes name_eml,
I have a SQL database that has a table with a field set to
I have a database table that has a Unique Key constraint defined to avoid

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.