MySQL / BigQuery - 加权平均值和分组依据

MySQL / BigQuery - Weighted Average & Group By

我正在尝试计算数据集的加权平均值和return最大值每月 在 12 个月内连同其相应的 票据描述

我知道有很多解决类似问题的问题,但我还没有找到结合我认为需要的语法的解决方案。

这是一些示例 table 数据:

Month_Begin_Date Priority ticket_about_tag Phone_Time Occurances
2019-02-01 Urgent Power Bill 22.42 36
2019-02-01 Normal Power Bill 3.41 89
2019-05-01 Normal Wifi Issue 45.32 12

这是我当前确定加权平均值的查询:

SELECT (Month_Begin_Date, 
(sum(phone_time * occurances))/sum(occurances)) AS Weighted_Average_Phone_Time
FROM database
GROUP BY month_begin_date

这 return 是所有 ticket_about_tags、每月 加权平均值 总数。

但我仍然需要得到这个,以便它显示 最大 加权平均值 票据描述。 IE。看起来像这样的东西:

Month_Begin_Date ticket_about_tag Weighted_average_phone_time
2019-01-01 Power Bill 22.42
2019-02-01 Power Bill 3.41
2019-03-01 Wifi Issue 45.32

我尝试将此作为子查询添加到另一个查询中,以便 return 我之后的数据,如下所示:

SELECT Month_Begin_date, Ticket_About_Tag, Phone_Average_Handle_Time 
FROM database WHERE CONCAT(month_begin_date,phone_time) IN 
(SELECT CONCAT (Month_Begin_Date, 
(sum(phone_time * occurances))/sum(occurances)) AS Weighted_Average_Phone_Time
FROM database
GROUP BY month_begin_date
)
ORDER BY month_begin_date ASC  

非常感谢您的帮助

不确定我是否答对了你的问题,但使用了以下数据:

Month_Begin_Date Priority Ticket_About_Tag Phone_Time Occurences
2019-02-01 Urgent Power Bill 22.42 36
2019-02-01 Normal Power Bill 3.41 89
2019-05-01 Normal Wifi Issue 45.32 12
2019-02-01 Urgent Wifi Issue 14.2 7
2019-02-01 Normal Wifi Issue 30.7 5

这是您要查询的内容吗?

SELECT 
Month_Begin_Date, Ticket_About_Tag,
SUM(Phone_Time * Occurences) / SUM(Occurences) AS Weighted_Average_Phone_Time
FROM `database`
GROUP BY Month_Begin_Date, Ticket_About_Tag
ORDER BY Month_Begin_Date ASC, Ticket_About_Tag ASC;

这会得到与您发布的结果类似的结果:

Month_Begin_Date Ticket_About_Tag Weighted_Average_Phone_Time
2019-02-01 Power Bill 8.884880083084106
2019-02-01 Wifi Issue 21.075000206629436
2019-05-01 Wifi Issue 45.31999969482422

回复您的评论

要回答您的评论,您可以:

SELECT
a.Month_Begin_Date, 
a.Ticket_About_Tag, 
b.Max_Weighted_Average_Phone_Time
FROM (

SELECT 
Month_Begin_Date,
Ticket_About_Tag,
SUM(Phone_Time * Occurences) / SUM(Occurences) AS Weighted_Average_Phone_Time
FROM `database`
GROUP BY Month_Begin_Date, Ticket_About_Tag

) a

LEFT JOIN (

SELECT
b1.Month_Begin_Date, 
MAX(b1.Weighted_Average_Phone_Time) AS Max_Weighted_Average_Phone_Time
FROM (

SELECT 
Month_Begin_Date,
Ticket_About_Tag,
SUM(Phone_Time * Occurences) / SUM(Occurences) AS Weighted_Average_Phone_Time
FROM `database`
GROUP BY Month_Begin_Date, Ticket_About_Tag

) b1

GROUP BY b1.Month_Begin_Date

) b ON a.Month_Begin_Date = b.Month_Begin_Date

WHERE a.Weighted_Average_Phone_Time = b.Max_Weighted_Average_Phone_Time

这将为您提供以下输出:

Month_Begin_Date Ticket_About_Tag Max_Weighted_Average_Phone_Time
2019-02-01 Wifi Issue 21.075000206629436
2019-05-01 Wifi Issue 45.31999969482422

还有其他方法可以做到这一点,但我认为这是迄今为止最容易理解的方法,无需使用其他 SQL 结构。它反映了您需要两次处理相同的数据,首先按月和票标签聚合,然后按月查找聚合数据的最大值。