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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:14:20+00:00 2026-05-28T01:14:20+00:00

Given a table with: ID VALUE — —– 1 1 2 2 3 3

  • 0

Given a table with:

    ID  VALUE
    --  -----
    1   1
    2   2
    3   3
    4   4

I would like to compute something like this:

    ID  VALUE  SUM
    --  -----  ---
    1   1       40     -- (2-1)*2 + (3-1)*3 + (4-1)*4 + (5-1)*5
    2   2       26     -- (3-2)*3 + (4-2)*4 + (5-2)*5
    3   3       14     -- (4-3)*4 + (5-3)*5
    4   4        5     -- (5-4)*5
    5   5        0     -- 0 

Where the SUM on each row is the sum of the values of each subsequent row multiplied by the difference between the value of the subsequent row and the current row.

I could start with something like this:

    CREATE TABLE x(id int, value int);

    INSERT INTO x VALUES(1, 1);
    INSERT INTO x VALUES(2, 2);
    INSERT INTO x VALUES(3, 3);
    INSERT INTO x VALUES(4, 4);
    INSERT INTO x VALUES(5, 5);

    SELECT id, value
          ,SUM(value) OVER(ORDER BY id ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) AS sum
      FROM x;

     id | value | sum
    ----+-------+-----
      1 |     1 |  14
      2 |     2 |  12
      3 |     3 |   9
      4 |     4 |   5
      5 |     5 |
    (5 rows)

where each row has the sum of all subsequent rows. But to take it further, I would really want something like this pseudo code:

    SELECT id, value
          ,SUM( (value - FIRST_ROW(value)) * value )
             OVER(ORDER BY id ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) AS sum
      FROM x;

But this is not valid. And that is the crux of the question: is there a way to reference multiple rows in the window of an analytic function? Or a different way to approach this? The example above is contrived. I was actually playing with an interesting puzzle from another post Rollup Query which led me to this problem. I am trying this in Postgresql 9.1, but not bound to that.

  • 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-28T01:14:20+00:00Added an answer on May 28, 2026 at 1:14 am

    Not quite sure if I’ve understood your requirement exactly here, but the query that you want is something like

    select a.id, a.value, sum(( b.value - a.value ) * b.value )
    from x a, x b
    where a.id < b.id
    group by a.id, a.value
    

    Hope that helps.

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

Sidebar

Related Questions

Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
Given a table named person (in a MySQL database/schema), kind of like this one:
Given a table like so: id | value ------------------- 1 | food 2 |
Given a table like this: [Last Name] [First Name] [DepartmentID] --------------------------------------- Doe John 1
Given a table like this, what query will the most recent calibration information for
I would like to optimize this query using SQLite 3. SELECT id FROM Table
I am trying to populate a row in a table given a key value
Given the following: declare @a table ( pkid int, value int ) declare @b
I need to query a table for values given a string. The table is
Given a table (mytable) containing a numeric field (mynum), how would one go about

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.