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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:17:40+00:00 2026-05-17T00:17:40+00:00

We have some tables, which have a structure like: start, — datetime end, —

  • 0

We have some tables, which have a structure like:

start, — datetime
end, — datetime
cost — decimal

So, for example, there might be a row like:

01/01/2010 10:08am, 01/01/2010 1:56pm, 135.00
01/01/2010 11:01am, 01/01/2010 3:22pm, 118.00
01/01/2010 06:19pm, 01/02/2010 1:43am, 167.00

Etc…

I’d like to get this into a format (with a function?) that returns data in a format like:

10:00am, 10:15am, X, Y, Z
10:15am, 10:30am, X, Y, Z
10:30am, 10:45am, X, Y, Z
10:45am, 11:00am, X, Y, Z
11:00am, 11:15am, X, Y, Z

….

Where:
X = the number of rows that match
Y = the cost / expense for that chunk of time
Z = the total amount of time during this duration

IE, for the above data, we might have:

10:00am, 10:15am, 1, (135/228 minutes*7), 7

  • The first row starts at 10:08am, so only 7 minutes are used from 10:00-10:15.
  • There are 228 minutes in the start->end time.

….

11:00am, 11:15am, 2, ((135+118)/((228+261) minutes*(15+14)), 29

  • The second row starts right after 11:00am, so we need 15 minutes from the first row, plus 14 minutes from the second row
  • There are 261 minutes in the second start->end time

….

I believe I’ve done the math right here, but need to figure out how to make this into a PG function, so that it can be used within a report.

Ideally, I’d like to be able to call the function with some arbitrary duration, ie 15minute, or 30minute, or 60minute, and have it split up based on that.

Any ideas?

  • 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-17T00:17:41+00:00Added an answer on May 17, 2026 at 12:17 am

    Here is my try. Given this table definition:

    CREATE TABLE interval_test
    (
      "start" timestamp without time zone,
      "end" timestamp without time zone,
      "cost" integer
    )
    

    This query seems to do what you want. Not sure if it is the best solution, though.
    Also note that it needs Postgres 8.4 to work, because it uses WINDOW functions and WITH queries.

    WITH RECURSIVE intervals(period_start) AS (
        SELECT 
        date_trunc('hour', MIN(start)) AS period_start
          FROM interval_test
    
      UNION ALL
        SELECT intervals.period_start + INTERVAL '15 MINUTES'
          FROM  intervals
          WHERE (intervals.period_start + INTERVAL '15 MINUTES') < (SELECT MAX("end") FROM interval_test)
      )
      SELECT DISTINCT period_start, intervals.period_start + INTERVAL '15 MINUTES' AS period_end, 
      COUNT(*) OVER  (PARTITION BY period_start ) AS record_count,
    SUM (LEAST(period_start + INTERVAL '15 MINUTES', "end")::timestamp - GREATEST(period_start, "start")::timestamp)
      OVER  (PARTITION BY period_start ) AS total_time,
    
      (SUM(cost) OVER  (PARTITION BY period_start ) /  
    
    
     (EXTRACT(EPOCH FROM SUM("end" - "start") OVER  (PARTITION BY period_start )) / 60)) * 
    
     ((EXTRACT (EPOCH FROM SUM (LEAST(period_start + INTERVAL '15 MINUTES', "end")::timestamp - GREATEST(period_start, "start")::timestamp)
      OVER  (PARTITION BY period_start )))/60)
    
       AS expense
    
    FROM  interval_test
    INNER JOIN intervals ON (intervals.period_start, intervals.period_start + INTERVAL '15 MINUTES') OVERLAPS (interval_test.start, interval_test.end)
    
    ORDER BY period_start ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.