Postgres 计算加权平均值
Postgres Calculating Weighted Average
我正在尝试从这个例子中获取项目的加权平均值 table:
Item
Value
Item1
0.10
Item2
0.15
我按照这个例子来计算它:https://www.wikihow.com/Calculate-Weighted-Average
并尝试了这个语句:
SELECT
(SELECT CASE
WHEN value <= 0.1 THEN 'very good'
WHEN value <= 0.15
value > 0.1 THEN 'good'
WHEN valuey <= 0.20
AND value > 0.15 THEN 'not that good'
WHEN value <= 0.25
AND value > 0.20 THEN 'bad'
ELSE 'very bad'
END ) AS ratings,
COUNT (*) AS numberOfItems,
COUNT (*) / SUM(DISTINCT ( SELECT COUNT(DISTINCT collateral_id)
FROM triggered.table
WHERE id = 386)) as weightingFactor,
(COUNT (*) * (COUNT (*) / SUM(DISTINCT ( SELECT COUNT(DISTINCT collateral_id)
FROM triggered.table
WHERE id = 386)))) as numberXWeightingFactor
FROM triggered.table
WHERE id = 386
GROUP BY stars
我试图通过 SUM(numberXWeightingFactor)
获得平均值,但它不起作用。最终给我错误:column "numberxweightingfactor" does not exist
.
多个问题。最重要的是,您似乎混淆了名称 ratings
和 stars
(我将两者都替换为 rating
),并且 window functions 不允许 DISTINCT
聚合。
这应该有效:
SELECT t.*
, t.number_of_items / c.ct_dist_coll AS weighting_factor
, t.number_of_items^2 / c.ct_dist_coll AS number_x_weighting_factor
FROM (
SELECT CASE
WHEN value <= 0.1 THEN 'very good'
WHEN value > 0.1 AND value <= 0.15 THEN 'good'
WHEN value > 0.15 AND value <= 0.20 THEN 'not that good'
WHEN value > 0.20 AND value <= 0.25 THEN 'bad'
ELSE 'very bad' END AS rating
, count(*) AS number_of_items
FROM triggered.table
WHERE id = 386
GROUP BY 1
) t
CROSS JOIN (
SELECT count(DISTINCT collateral_id) AS ct_dist_coll
FROM triggered.table
WHERE id = 386
) c;
我正在尝试从这个例子中获取项目的加权平均值 table:
Item | Value |
---|---|
Item1 | 0.10 |
Item2 | 0.15 |
我按照这个例子来计算它:https://www.wikihow.com/Calculate-Weighted-Average
并尝试了这个语句:
SELECT
(SELECT CASE
WHEN value <= 0.1 THEN 'very good'
WHEN value <= 0.15
value > 0.1 THEN 'good'
WHEN valuey <= 0.20
AND value > 0.15 THEN 'not that good'
WHEN value <= 0.25
AND value > 0.20 THEN 'bad'
ELSE 'very bad'
END ) AS ratings,
COUNT (*) AS numberOfItems,
COUNT (*) / SUM(DISTINCT ( SELECT COUNT(DISTINCT collateral_id)
FROM triggered.table
WHERE id = 386)) as weightingFactor,
(COUNT (*) * (COUNT (*) / SUM(DISTINCT ( SELECT COUNT(DISTINCT collateral_id)
FROM triggered.table
WHERE id = 386)))) as numberXWeightingFactor
FROM triggered.table
WHERE id = 386
GROUP BY stars
我试图通过 SUM(numberXWeightingFactor)
获得平均值,但它不起作用。最终给我错误:column "numberxweightingfactor" does not exist
.
多个问题。最重要的是,您似乎混淆了名称 ratings
和 stars
(我将两者都替换为 rating
),并且 window functions 不允许 DISTINCT
聚合。
这应该有效:
SELECT t.*
, t.number_of_items / c.ct_dist_coll AS weighting_factor
, t.number_of_items^2 / c.ct_dist_coll AS number_x_weighting_factor
FROM (
SELECT CASE
WHEN value <= 0.1 THEN 'very good'
WHEN value > 0.1 AND value <= 0.15 THEN 'good'
WHEN value > 0.15 AND value <= 0.20 THEN 'not that good'
WHEN value > 0.20 AND value <= 0.25 THEN 'bad'
ELSE 'very bad' END AS rating
, count(*) AS number_of_items
FROM triggered.table
WHERE id = 386
GROUP BY 1
) t
CROSS JOIN (
SELECT count(DISTINCT collateral_id) AS ct_dist_coll
FROM triggered.table
WHERE id = 386
) c;