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 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

Ask A Question

Stats

  • Questions 539k
  • Answers 539k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer this inside your success-function does not have the same scope… May 17, 2026 at 2:16 am
  • Editorial Team
    Editorial Team added an answer I believe you want something like this: CREATE TABLE property… May 17, 2026 at 2:16 am
  • Editorial Team
    Editorial Team added an answer Java's java.util.Scanner breaks up its input based on some delimiter.… May 17, 2026 at 2:16 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

My Table structure have two Columns, One for start range and another for End
I have a JTable which is loaded from a data-structure using table model.The data-structure
I have a table in Sql Server 2008 Express which contains 18 million records.
I have a table containing hierarchical data. There are currently ~8 levels in this
I'm building an analytics system for a mobile application and have had some difficulty
Problem I have a YQL query result that I'm trying to get converted and
I have been googling a lot and I couldn't find if this even exists
For quite a simple table structure, ie. Person, Criteria, and PersonCriteria (the combi-table), I
I'm playing around at the start of a personal project in C# and MySQL.
I have postgres database running on Amazon EC2 instance. I have few tablespaces created

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.