Question

I have a table table1 with three columns a, b, c. I am creating another column by doing a group by on c and some function func(a,b) as d giving me view1. In order to add the column d to table1, the only thing I can think of is to perform a join between view1 and table1. However, both of them have millions of rows and it gets really slow. Is there any other way without joining them? It looks intuitively that it should be possible.

Here is a snippet of the script

with 

found_mean
as
(select sum(count*avg)/sum(count) as combined_avg , b from view_1 group by b),

view_1_m
as
(select combined_avg , count , avg, variance , found_mean.b from found_mean , view_1 where       found_mean.b = view_1.b),
Était-ce utile?

La solution

Depending on what your function is, you can use window functions (sometimes called analytic functions). For instance, if you wanted the maximum value of b for a given a:

select a, b, c, max(b) over (partition by a) as d
from table1;

Without more information, it is hard to be more specific.

EDIT:

You should be able to do this with analytic functions:

select count , avg, variance,
       (sum(count * avg) over (partition by b) /
        sum(count) over (partition by b)
       ) as weighted_average
from view_1;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top