简体   繁体   English

计算 SQL Server 中的平均值

[英]Calculating an average of averages in SQL Server

I want to do something very simple but I'm obviously missing a trick!我想做一些非常简单的事情,但我显然错过了一个技巧! I want to get an average of average values but I want to include the weighting of the original average calculation.我想获得平均值的平均值,但我想包括原始平均计算的权重。 I'll use a stripped back version of what I'm attempting to do.我将使用我正在尝试做的精简版本。

So let's say I have the following table所以假设我有下表

Product     date        RunInterval    AvgDuration_secs   Executions
--------------------------------------------------------------------
   A        29/12/19            1               1           100
   A        29/12/19            2               2            10

What I want to find out is what the average duration was for Product A on 29/12.我想知道的是 29/12 产品 A 的平均持续时间是多少。 All the things I've tried so far are giving me an average of 1.5 secs ie it's adding together the duration of 1 & 2 secs (3) and dividing by number of rows (2) to give 1.5.到目前为止,我尝试过的所有事情平均给了我 1.5 秒,即它将 1 和 2 秒 (3) 的持续时间加在一起并除以行数 (2) 得到 1.5。 What I want to get to is to have the average but taking into account how often it runs so (100*1) + (10*2) / 110 = 1.09 secs.我想要得到的是平均值,但考虑到它运行的频率,所以 (100*1) + (10*2) / 110 = 1.09 秒。 I've tried various attempts with GROUP BY statements and CURSORS but not getting there.我已经尝试了 GROUP BY 语句和 CURSORS 的各种尝试,但没有成功。

I'm evidently tackling it the wrong way!我显然以错误的方式处理它! Any help welcome :-)欢迎任何帮助:-)

You can do it like this:你可以这样做:

select product, date,
  round(1.0 * sum([Executions] * [AvgDuration_secs]) / sum([Executions]), 2) result
from tablename
group by product, date

I'm not sure if you want RunInterval or AvgDuration_secs in the 1st sum.我不确定您是否希望在第一个总和中使用RunIntervalAvgDuration_secs
See the demo .请参阅演示
Results:结果:

> product | date      | result        
> :------ | :---------| :-----
> A       | 29/12/2019| 1.09

If you got those results from a query or view that select from some table, then grouped by Product & date & RunInterval.如果您从某个表中选择的查询或视图中获得这些结果,则按产品、日期和运行间隔分组。

Then you could simply run a query that on that table that groups only by the Product & date.然后您可以简单地在该表上运行一个查询,该查询仅按产品和日期分组。

An example:一个例子:

 -- -- Sample data -- CREATE TABLE sometable ( Product varchar(30), ExecutionDatetime datetime, RunInterval int ); WITH RCTE_NUMS AS ( SELECT 1 AS n UNION ALL SELECT n+1 FROM RCTE_NUMS WHERE n < 110 ) INSERT INTO sometable (Product, ExecutionDatetime, RunInterval) SELECT 'A' p, DATEADD(minute,n*12,'2019-12-29 01:00:00') dt, IIF(n<=100,1,2) ri FROM RCTE_NUMS OPTION (MAXRECURSION 1000);
\n110 rows affected 110 行受影响\n
select Product, cast(ExecutionDatetime as date) as [Date], AVG(1.0*RunInterval) AS AvgDuration_secs, COUNT(*) AS Executions from sometable t group by Product, cast(ExecutionDatetime as date) ORDER BY Product, [Date]
\nProduct |产品 | Date |日期 | AvgDuration_secs | AvgDuration_secs | Executions处决\n:------ | :------ | :------------------ | :------------------ | :--------------- | :--------------- | ---------: ---------:\nA |一个 | 29/12/2019 00:00:00 | 29/12/2019 00:00:00 | 1.090909 | 1.090909 | 110 110\n

db<>fiddle here db<> 在这里摆弄

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

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