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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:52:58+00:00 2026-06-13T18:52:58+00:00

I have a table with entity name, year and activity number as bellow. During

  • 0

I have a table with entity name, year and activity number as
bellow. During some years there is not any activity.

name | year | act_num
-----+------+---------
aa   | 2000 |       2
aa   | 2001 |       6
aa   | 2002 |       9
aa   | 2003 |      15
aa   | 2005 |      17
b    | 2000 |       3
b    | 2002 |       4
b    | 2003 |       9
b    | 2005 |      12
b    | 2006 |       2

To create it on postgresql;

CREATE TABLE entity_year_activity (
name character varying(10),
year integer,
act_num integer
);

INSERT INTO entity_year_activity
VALUES
    ('aa', 2000, 2),
    ('aa', 2001, 6),
    ('aa', 2002, 9),
    ('aa', 2003, 15),
    ('aa', 2005, 17),
    ('b', 2000, 3),
    ('b', 2002, 4),
    ('b', 2003, 9),
    ('b', 2005, 12),
    ('b', 2006, 2);

I would like to have the total number of the past x years with the
number of this year activities for each entity for every year as bellow.

As an example for x = three years.

name | year | act_num | total_3_years
-----+------+---------+---------------
aa   | 2000 |       2 |      2
aa   | 2001 |       6 |      8
aa   | 2002 |       9 |     17
aa   | 2003 |      15 |     30
aa   | 2004 |       0 |     24
aa   | 2005 |      17 |     32
b    | 2000 |       3 |      3
b    | 2001 |       0 |      3
b    | 2002 |       4 |      7
b    | 2003 |       9 |     13
b    | 2005 |      12 |     21
b    | 2006 |       2 |     14
  • 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-13T18:52:59+00:00Added an answer on June 13, 2026 at 6:52 pm

    Here’s an approach that uses the ability to use the sum aggregate as a window function with a range-based window frame – see SUM(...) OVER (PARTITION BY name ORDER BY year ROWS 2 PRECEDING) and window framing.

    WITH name_years(gen_name, gen_year) AS (
      SELECT gen_name, s
      FROM generate_series(
        (SELECT min(year) FROM entity_year_activity),
        (SELECT max(year) FROM entity_year_activity)
      ) s CROSS JOIN (SELECT DISTINCT name FROM entity_year_activity) n(gen_name)
    ),
    windowed_history(name, year,act_num,last3_actnum) AS (
      SELECT
        gen_name, gen_year, coalesce( act_num, 0),
        SUM(coalesce(act_num,0)) OVER (PARTITION BY gen_name ORDER BY gen_year ROWS 2 PRECEDING)
      FROM name_years 
      LEFT OUTER JOIN entity_year_activity ON (gen_name = name AND gen_year = year)
    )
    SELECT name, year, act_num, sum(last3_actnum) as total_3_years
    FROM windowed_history
    GROUP BY name, year, act_num
    HAVING sum(last3_actnum) <> 0
    ORDER BY name, year;
    

    See SQLFiddle.

    The need to generate entries for years that have no entry themselves complicates this query. I generate a table of all (name, year) pairs, then left outer join entity_year_activity on it before doing the window sum, so all years for all name sets are represented. That’s why this is so complicated. Then I filter the aggregated result to exclude entries with zero in the sum.

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

Sidebar

Related Questions

I have the following mapping: @Entity @Table(name = Prequalifications) public class Prequalification implements Serializable
I have an entity mapped as following: @Entity @Table(name = Groups) @IdClass(value = GroupId.class)
I have this piece of code: @Entity @Table(name = MOVERS) public class MOVers implements
I have the following entity class : @Entity @Table(name = THE_TREE, catalog = ,
I have the following entity with the following relation: @Entity @Table(name = bpr_user) public
Hi every one I have these classe @Entity @Table(name = login, uniqueConstraints={@UniqueConstraint(columnNames={username_fk})}) public class
I have two entities // All details has been removed @Entity @Table(name = "A_TABLE_NAME")
I have an entity defined as follows @Entity(name = Report) @Table(name = REPORTS) @org.hibernate.annotations.Entity(dynamicInsert
I have code like: @Entity @Table(name = A) @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public class A
I have the following (simplified) Hibernate entities: @Entity @Table(name = package) public class Package

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.