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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:40:27+00:00 2026-06-13T10:40:27+00:00

I have a table like this in PostgreSQL. I want to perform aggregation functions

  • 0

I have a table like this in PostgreSQL. I want to perform aggregation functions like mean and max for every 16 records based on ID (which is primary key). For example I have to calculate mean value for first 16 records and second 16 records and so on.

+-----+-------------
| ID  |  rainfall  |
+-----+----------- |
|  1  |  110.2     |
|  2  |  56.6      |
|  3  |  65.6      |
|  4  |  75.9      |
+-----+------------
  • 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-13T10:40:28+00:00Added an answer on June 13, 2026 at 10:40 am

    The 1st approach that comes to mind is to use row_number() to annotate the table, then group by blocks of 16 rows.

    SELECT min(id) as first_id, max(id) AS last_id, avg(rainfall) AS avg_this_16
    FROM (
      SELECT id, rainfall, row_number() OVER (order by id) AS n
      FROM the_table
    ) x(id,rainfall,n)
    GROUP BY n/16
    ORDER BY n/16;
    

    Note that this won’t necessarily include 16 samples for the last group.

    Alternately you can calculate a running average by using avg() as a window function:

    SELECT id, avg(rainfall) OVER (ORDER BY id ROWS 15 PRECEDING)
    FROM the_table;
    

    … possibly annotating that with the row number and selecting the ones you want:

    SELECT id AS greatest_id_in_group, avg_last_16_inclusive FROM (
      SELECT
        id, 
        avg(rainfall) OVER (ORDER BY id ROWS 15 PRECEDING) AS avg_last_16_inclusive,
        row_number() OVER (ORDER BY id) AS n
      FROM the_table
    ) x WHERE n % 16 = 0;
    

    This will disregard the last n<16 samples, not returning a row for them.

    Note that I’m assuming the IDs aren’t guaranteed to be contiguous. If they are gap-less, you can just group by id/16 and avoid the window function.

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

Sidebar

Related Questions

I have a table in my postgresql 8.4 database like this: id(serial), event_type_id(id, foreign
I have a table with 3 columns which looks like this: File User Rating
I have a table in PostgreSQL DB like this: Client | Rate | StartDate|EndDate
I am using PostgreSQL 8.3 . I have a table like this: id regist_time
I have a table in PostgreSQL where the schema looks like this: CREATE TABLE
I have table like this : ID Name_1 Name_2 Name_3 1 Egon Spengler Ives
I have a table like this ID Name IsDeleted 1 Yogesh Null 2 Goldy
I have a table like this : +-------+------------+------+-----+---------+-------+ | Field | Type | Null
I have a table like this: ID country ------------- 1 US 2 Japan 3
I have a table like this: id | id_parent | tag_name 0 | |

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.