简体   繁体   English

SQL 多行加权平均值 -

[英]SQL Weighted averages of multiple rows -

How would you go about to find the real weighted average of multiple rows:您将如何 go 找到多行的实际加权平均值:

By real weighted, I mean like this calculator: https://www.rapidtables.com/calc/math/weighted-average-calculator.html (and not by multiplying value with weight).通过实际加权,我的意思是像这个计算器: https://www.rapidtables.com/calc/math/weighted-average-calculator.html (而不是通过将值乘以权重)。

在此处输入图像描述

The weight for each answer is set in answers and values for each question in child-table answer_items .每个答案的权重在子表answer_items中每个问题的answers和值中设置。 We want the weighted average for each question (a,b,c,d).我们想要每个question的加权平均值 (a,b,c,d)。 We know what questions to look for in advance.我们知道要提前寻找哪些问题。

The query will include between 2 and 500k answers (so preferably a speedy solution:) )查询将包括 2 到 500k 个答案(因此最好是快速解决方案:))

CREATE TABLE `answers` (
  `id` int(10) NOT NULL,
  `weight` varchar(255) NOT NULL
);

INSERT INTO `answers` (`id`, `weight`) VALUES
(1, '0.7'),
(2, '1'),
(3, '0.7'),
(4, '0.9');

CREATE TABLE `answer_items` (
  `id` int(11) NOT NULL,
  `answer_id` int(11) NOT NULL,
  `question` varchar(5) NOT NULL,
  `value` int(11) NOT NULL
);

INSERT INTO `answer_items` (`id`, `answer_id`, `question`, `value`) VALUES
(1, 1, 'a', 2),
(2, 1, 'b', 4),
(3, 1, 'c', 2),
(4, 1, 'd', 3),
(5, 2, 'a', 4),
(6, 2, 'b', 2),
(7, 2, 'c', 4),
(8, 2, 'd', 1),
(9, 3, 'a', 3),
(10, 3, 'b', 4),
(11, 3, 'c', 1),
(12, 3, 'd', 5),
(13, 4, 'a', 5),
(14, 4, 'b', 2),
(15, 4, 'c', 3),
(16, 4, 'd', 3);


ALTER TABLE `answers`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `answer_items`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `answers`
  MODIFY `id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;

ALTER TABLE `answer_items`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=17;

You can multiply the value times the weight and then divide by the sum of the weights.您可以将值乘以权重,然后除以权重的总和。 For the weighted average by question:对于问题的加权平均值:

select question, sum(ai.value * a.weight) / sum(a.weight)
from answer_items ai join
     answers a
     on ai.answer_id = a.id
group by question;

Here is a db<>fiddle. 是一个 db<>fiddle。

Somethin like:像这样的东西:

SELECT
   SUM(value * weight) / SUM (weight)
FROM
   answers
JOIN
   answer_items
ON
   answer_items.answer_id = answers.id

(edit: and obviously include the question and GROUP BY question, as in the previous answer for the SUM aggregation, as per the previous answer. D'oh) (编辑:显然包括问题和 GROUP BY 问题,如上一个 SUM 聚合的答案,根据上一个答案。D'oh)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM