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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:18:38+00:00 2026-05-26T19:18:38+00:00

I am working on a Java implementation for temporal aggregation using a PostgreSQL database.

  • 0

I am working on a Java implementation for temporal aggregation using a PostgreSQL database.

My table looks like this

Value | Start      | Stop
(int) | (Date)     | (Date)
-------------------------------
1     | 2004-01-01 | 2010-01-01
4     | 2000-01-01 | 2008-01-01

So to visualize this periods:

                      ------------------------------
  ----------------------------------------
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
  [        4         ][       5=4+1      ][    1   ]

My algorithm now calculates temporal aggregations of the data, e.g. SUM():

Value | Start      | Stop
-------------------------------
4     | 2000-01-01 | 2004-01-01
5     | 2004-01-01 | 2008-01-01
1     | 2008-01-01 | 2010-01-01

In order to test the gained results, I now would like to query the data directly using PostgreSQL. I know that there is no easy way to this problem, yet. However, there surely is a way to get the same results. The aggregations Count, Max, Min, Sum and Average should be supported. I do not mind a bad or slow solution, it just has to work.

A query I found so far which should work similarly is the following:

select count(*), ts, te
from ( checkout a normalize checkout b using() ) checkoutNorm
group by ts, te;

My adoption looks like this:

select count(*), start, stop
from ( myTable a normalize myTable b using() ) myTableNorm
group by start, stop;

However, an error was reported ERROR: syntax error at or near "normalize" -- LINE 2: from ( ndbs_10 a normalize ndbs_10 b using() ) ndbsNorm.

Does anyone has a solution to this problem? It does not have to be based on the above query, as long as it works. Thanks a lot.

  • 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-26T19:18:39+00:00Added an answer on May 26, 2026 at 7:18 pm

    Your question was really hard to understand. But I think I figured it out.
    You want a running sum over value. Values are only applicable between start and stop of a time period. So they have to be added at the begin of that period and deducted at the end.
    In addition you want the begin and end of the resulting period the sum is valid for.
    That should do it:

    -- DROP SCHEMA x CASCADE;
    CREATE SCHEMA x;
    CREATE TABLE x.tbl(val int, start date, stop date);
    INSERT INTO x.tbl VALUES
     (4 ,'2000-01-01' ,'2008-01-01')
    ,(7 ,'2001-01-01' ,'2009-01-01')
    ,(1 ,'2004-01-01' ,'2010-01-01')
    ,(2 ,'2005-01-01' ,'2006-01-01');
    
    WITH a AS (
        SELECT start as ts, val FROM x.tbl
        UNION  ALL
        SELECT stop, val * (-1) FROM x.tbl
        ORDER  BY 1, 2)
    SELECT sum(val) OVER w AS val_sum
          ,ts AS start
          ,lead(ts) OVER w AS stop
    FROM   a
    WINDOW w AS (ORDER BY ts)
    ORDER  BY ts;
    
    val_sum |   start    |    stop
    --------+------------+------------
          4 | 2000-01-01 | 2001-01-01
         11 | 2001-01-01 | 2004-01-01
         12 | 2004-01-01 | 2005-01-01
         14 | 2005-01-01 | 2006-01-01
         12 | 2006-01-01 | 2008-01-01
          8 | 2008-01-01 | 2009-01-01
          1 | 2009-01-01 | 2010-01-01
          0 | 2010-01-01 |
    

    Edit after request

    For all requested aggregate functions:

    SELECT period
          ,val_sum
          ,val_count
          ,val_sum::float /val_count AS val_avg
          ,(SELECT min(val) FROM x.tbl WHERE start < y.stop AND stop > y.start) AS val_min
          ,(SELECT max(val) FROM x.tbl WHERE start < y.stop AND stop > y.start) AS val_max
          ,start
          ,stop
    FROM   (
        WITH a AS (
             SELECT start as ts, val, 1 AS c FROM x.tbl
             UNION  ALL
             SELECT stop, val, -1 FROM x.tbl
             ORDER  BY 1, 2)
        SELECT count(*) OVER w AS period
              ,sum(val*c) OVER w AS val_sum
              ,sum(c) OVER w AS val_count
              ,ts AS start
              ,lead(ts) OVER w AS stop
        FROM   a
        WINDOW w AS (ORDER BY ts)
        ORDER  BY ts
        ) y
    WHERE stop IS NOT NULL;
    
     period | val_sum | val_count | val_avg | val_min | val_max |   start    |    stop
    --------+---------+-----------+---------+---------+---------+------------+------------
          1 |       4 |         1 |       4 |       4 |       4 | 2000-01-01 | 2001-01-01
          2 |      11 |         2 |     5.5 |       4 |       7 | 2001-01-01 | 2004-01-01
          3 |      12 |         3 |       4 |       1 |       7 | 2004-01-01 | 2005-01-01
          4 |      14 |         4 |     3.5 |       1 |       7 | 2005-01-01 | 2006-01-01
          5 |      12 |         3 |       4 |       1 |       7 | 2006-01-01 | 2008-01-01
          6 |       8 |         2 |       4 |       1 |       7 | 2008-01-01 | 2009-01-01
          7 |       1 |         1 |       1 |       1 |       1 | 2009-01-01 | 2010-01-01
    

    min() and max could possibly be optimized, but that should be good enough.
    CTE (WITH clause) and and subqueries are exchangeable, as you can see.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on Mangler's Android implementation. I have a java class that looks
Since I'm using Java 1.4.2, this means that I cannot use Java's implementation of
I've been working on a Fibonacci Heap implementation in Java for about a week
I am working on a chat implementation with Java sockets. I have focused on
I am working with google app engine and using the low leval java api
I currently have a working, simple language implemented in Java using ANTLR. What I
This is language agnostic, but I'm working with Java currently. I have a class
I'm working on role-based security implementation in LDAP and Java. Specifically, I have the
I'm working on a database design for a Java web application work flow app
Weird one this, Both my Java and C code seem to be working fine.

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.