Skip to content
Advertisement

Postgres Calculating Weighted Average

I am trying to get the weighted average of items from this example table:

Item Value
Item1 0.10
Item2 0.15

I followed this example here how to calculate it: https://www.wikihow.com/Calculate-Weighted-Average

And tried this statement:

I am trying to get the average by doing SUM(numberXWeightingFactor) but it doesn’t work. Ends up giving me error: column "numberxweightingfactor" does not exist.

Advertisement

Answer

Multiple problems. Most importantly, you seem to confuse the names ratings and stars (I replaced both with rating), and window functions do not allow DISTINCT aggregation.

This should work:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement