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,…}
Try something like this:
This
SELECTwill give you 300 records perarray300with sameidand assing them thebucket_num(1 for firs 3 elements, 2 for next 3, and so on).Then use this select to get the
avgof elements in the bucket:Next – just aggregate the
avg_valinto array:Details: unnest , ntile , array_agg , OVER (PARTITION BY )
UPD: Try this function:
It takes one
array300and outputs onearray100. To use it:If you have any problems understanding it – just ask me.