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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:20:43+00:00 2026-05-31T19:20:43+00:00

I have the following table in my postgreSQL 8.3.14 database timestamp status 2012-03-12 19:15:01

  • 0

I have the following table in my postgreSQL 8.3.14 database

     timestamp           status
2012-03-12 19:15:01        f
2012-03-12 19:15:02        f
2012-03-12 19:15:05        f
2012-03-12 19:17:01        t
-- END OF SLOT ONE (change from f to t)
2012-03-12 19:20:01        f
2012-03-12 19:25:01        f
2012-03-12 19:27:01        f
2012-03-12 20:15:01        t
-- END OF SLOT TWO (change from f to t)

No I want to get the following result:

  • slot 1 duration (2012-03-12 19:17:01 – 2012-03-12 19:15:01)
  • slot 2 duration (2012-03-12 20:15:01 – 2012-03-12 19:20:01)

So every time the status value changes from f to t, a new slot begins. Now I want to get all slots and the duration of every slot. The timestamp is not every second or minute or so, the difference between two entries is more or less random.

I thought about subqueries and grouping, but I have no idea how to solve this, at least whether it is possible at all. So my question is: is that possible 🙂 And if it is, how to start? I’m stuck because of the f-t changing issue…

  • 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-31T19:20:44+00:00Added an answer on May 31, 2026 at 7:20 pm

    If you have a modern version of PG you can do what you want using window functions (in my example instead of timestamps I have integers, but it doesn’t matter):
    I’m working on a non-window function version…

    db=> select * from test1;
     time | status
    ------+--------
    1 | f
    2 | f
    3 | f
    4 | t
    5 | f
    6 | f
    7 | f
    8 | t
    9 | f
    10 | t
    
    (8 rows)
    

    Here is the query with window functions:

     WITH a AS (
     SELECT time,rank() OVER (ORDER BY time) 
          FROM (
                   SELECT status, time, lag(status,1)  OVER (ORDER BY time) AS ls
                FROM test1 
                ) AS x
                 WHERE status AND NOT ls 
                -- select where the current status is true, the previous is false
           ),  -- a is now the endings of the slots
     b AS  (
         SELECT time, rank() OVER (ORDER BY time)
           FROM (
                 SELECT  status, time, LAG(status,1)         
                   OVER  (ORDER BY time)  AS ls
                 FROM test1 
                ) AS x 
                WHERE (NOT status AND ls) OR (NOT status AND ls IS NULL) 
            )   -- b is now the beginning of the slots
        SELECT b.time as time1, a.time as time2 FROM a,b WHERE a.rank=b.rank;
    
      time1 | time2
    ------+------
    1 |    4
    5 |    8
    9 |   10
    

    Here is the version of the query for PG without window functions (it is actually shorter, but less clear)

      SELECT min(time) as time1, time2 FROM 
         (
             SELECT time, status,
              (
               SELECT time FROM test1 AS y
                   WHERE y.time >= x.time
                   ORDER BY (x.status = y.status)::int, y.time ASC LIMIT 1
              ) AS time2 
            FROM test1 AS x WHERE NOT status
         ) AS y GROUP BY y.time2 ORDER BY time2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table and sequence in my postgresql-8.4 database: CREATE TABLE complexobjectpy
I have the following table in a database. I want to be able to
Suppose we have a PostgreSQL database with two tables A, B. table A columns:
I'd need advice on following situation with Oracle/PostgreSQL: I have a db table with
In PostgreSQL 8.3, let's say I have a table called widgets with the following:
I have following table Id | Status | date ------------------- 1 | Onsite |2007
I have the following table relationship in my database: Parent / \ Child1 Child2
I have the following database table with information about people, diseases, and drugs: PERSON_T
I have a table with two columns: latitude and longitude. I want to get
I have executed the following create statement using SQLWorkbench at my target postgresql database:

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.