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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:15:54+00:00 2026-06-15T16:15:54+00:00

I have a table like this: +—–+—————-+ | ID | array300 | +—–+—————-+ |

  • 0

I have a table like this:

+-----+----------------+
| ID  |  array300      |
+-----+----------------+
| 100 | {110,25,53,..} |
| 101 | {56,75,59,...} |
| 102 | {65,93,82,...} |
| 103 | {75,70,80,...} |
+-----+----------------+

array300 column is an array of 300 elements. I need to have arrays of 100 elements with every element representing the average of 3 elements of array300. For this example the answer will be like:
array100
{62.66,…}
{63.33,…}
{80,…}
{78.33,…}

  • 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-15T16:15:56+00:00Added an answer on June 15, 2026 at 4:15 pm

    Try something like this:

    SELECT id, unnest(array300) as val, ntile(100) OVER (PARTITION BY id) as bucket_num
    FROM your_table
    

    This SELECT will give you 300 records per array300 with same id and assing them the bucket_num (1 for firs 3 elements, 2 for next 3, and so on).

    Then use this select to get the avg of elements in the bucket:

    SELECT id, avg(val) as avg_val
    FROM (...previous select here...)
    GROUP BY id, bucket_num
    

    Next – just aggregate the avg_val into array:

    SELECT id, array_agg(avg_val) as array100
    FROM (...previous select here...)
    GROUP BY id
    

    Details: unnest , ntile , array_agg , OVER (PARTITION BY )

    UPD: Try this function:

    CREATE OR REPLACE FUNCTION public.array300_to_100 (
      p_array300 numeric []
    )
    RETURNS numeric [] AS
    $body$
    DECLARE
      dim_start int = array_length(p_array300, 1); --size of input array
      dim_end int = 100; -- size of output array
      dim_step int = dim_start / dim_end; --avg batch size
      tmp_sum NUMERIC; --sum of the batch
      result_array NUMERIC[100]; -- resulting array
    BEGIN
    
      FOR i IN 1..dim_end LOOP --from 1 to 100.
        tmp_sum = 0;
    
        FOR j IN (1+(i-1)*dim_step)..i*dim_step LOOP --from 1 to 3, 4 to 6, ...
          tmp_sum = tmp_sum + p_array300[j];  
        END LOOP; 
    
        result_array[i] = tmp_sum / dim_step;
      END LOOP; 
    
      RETURN result_array;
    END;
    $body$
    LANGUAGE 'plpgsql'
    IMMUTABLE
    RETURNS NULL ON NULL INPUT;
    

    It takes one array300 and outputs one array100. To use it:

    SELECT id, array300_to_100(array300)
    FROM table1;
    

    If you have any problems understanding it – just ask me.

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

Sidebar

Related Questions

I have table like this: id number value 1 300 233 2 343 434
I have a table like this with a computed column: CREATE TABLE PhoneNumbers (
I have table like this -> http://jsfiddle.net/jurisKaste/FvgeQ/ And I need to get array of
I have a table like this (Oracle, 10) Account Bookdate Amount 1 20080101 100
I have a table like this: <table id=myTable cellspacing=0 cellpadding=10 border=0 width=100%> <tbody> ....
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.