In postgres, when I call a function on some data, like so:
select f(col_nums) from tbl_name
where col_str = '12345'
then function f will be applied on each row where col_str = '12345'.
On the other hand, if I call an aggregation function on some data, like so:
select g_agg(col_nums) from tbl_name
where col_str = '12345'
then the function g_agg will be called on the the entire column but will result in a single value.
Q: How can I make a function that will be applied on the entire column and return a column of the same size while at the same time being aware of all the values in the the subset?
For example, can I create a function to calculate cumulative sum?
select *, sum_accum(col_nums) as cs from tbl_name
where col_str = '12345'
such that the result of the above query would look like this:
col_str | more_cols | col_numbers | cs
---------+-----------+-------------+----
12345 | 567 | 1 | 1
12345 | 568 | 2 | 3
12345 | 569 | 3 | 6
12345 | 570 | 4 | 10
Is there no choice but to pass a sub-query result to a function and then join with the original table?