简体   繁体   English

SQL计算属性之和的平均值

[英]SQL Average of Sum of Calculated Attribute

I am trying to take the average of the values for a category, where the rows are grouped by sub-category with a calculated sum. 我正在尝试获取类别值的平均值,其中按子类别将行与计算得出的总和分组。 The Primary key of the Parent Table is the grouped attribute of the Child Table. 父表的主键是子表的分组属性。 The grouped attribute of the Parent Table is neither the primary key or in the Child Table. 父表的分组属性既不是主键也不在子表中。

Simple representation: 简单表示:

select Category, avg(CalculatedSum)
from ParentTable pt
inner join (
    select Subcategory, sum(Quantity * Price) as 'CalculatedSum'
    from ChildTable
    group by Subcategory
    ) ct
on pt.ID = ct.Subcategory
group by Category

The actual SQL is as follows: 实际的SQL如下:

select c.CU_AGE_RANGE, count(*) as '# of Customers', avg(SumSales) as 'Avg of SumSales', max([Max of SumSales]) as 'Max of SumSales', min([Min of SumSales]) as 'Min of SumSales'
from Customers c
inner join (
    select CUSTOMER_ID, sum(QTY_SOLD * SALES) as SumSales, max(QTY_SOLD*SALES) as 'Max of SumSales', min(QTY_SOLD*SALES) as 'Min of SumSales'
    from Sales
    where (SALES > 0) and (QTY_SOLD > 0) and (COST > 0)
    Group by CUSTOMER_ID
    ) s
on c.CUSTOMER_ID = s.CUSTOMER_ID
group by c.CU_AGE_RANGE

I have tried changing the group by clause to various orders of the Category (CU_AGE_RANGE) and Subcategory (CUSTOMER_ID) but am always having the same error. 我尝试将group by子句更改为Category(CU_AGE_RANGE)和Subcategory(CUSTOMER_ID)的各种顺序,但是始终遇到相同的错误。

The error is that the table will always show me the SUM of the SUMS (I believe). 错误是该表将始终向我显示SUMS的总和(我相信)。 I am assuming this is the error because the typical average in the Child Table is 250 to 1000, and the Avg(Sum()) is returning values that are roughly the number of rows per Category times the expected Sum(). 我假设这是错误,因为子表中的典型平均值是250到1000,并且Avg(Sum())返回的值大约是每个类别的行数乘以预期的Sum()。

I cannot post a photo due to low reputation, so please see the following Comma Delimited Results Table: 由于信誉不佳,我无法发布照片,因此请参见以下逗号分隔结果表:

CU_AGE_RANGE,#_of_Customers,Avg_of_SumSales,Max_of_SumSales,Min_of_SumSales
NULL,125,4261665.306,433460737.7,0.0017
20-29     ,1192,1154040.907,1374037708,0.00025
30-39     ,1902,25429.52329,29426212.64,0.00015
40-49     ,2118,2418.829874,2066725,0.0001
50-59     ,2204,114625.4111,248240261.3,0.00015
60+       ,2135,160156.4341,334617675,0.0005
patrickbig,1,65.5737,12,0.06
Under 19  ,484,1431.262112,92160,0.0001

I am trying to figure out why the AVG(SUM()) is returning what seems to be the SUM(SUM()). 我试图弄清楚为什么AVG(SUM())返回的似乎是SUM(SUM())。 My current hunch is that since the SUM() is of a calculated entry, the calculated value is recalculated based on the grouping in the Parent Table. 我目前的预感是,由于SUM()是计算所得的条目,因此将基于父表中的分组重新计算计算所得的值。 So this would be: 因此,这将是:

DESIRED: 期望的:

x * y for each row in Child Table
sum(x*y) for each Subcategory
Avg(sum(x/y)) for each Category of Subcategory

QTY_SOLD * SALE for each row in Sales
sum(QTY_SOLD*SALE) for each CUSTOMER_ID
avg(sum(QTY_SOLD*SALE) for each CU_AGE_RANGE group of CUSTOMER_IDs

ACTUAL: 实际:

x * y for each row in Child Table                  
sum(x * y) for each Subcategory
avg(sum(x * y) for each Category

avg(sum(QTY_SOLD*SALE) for each CU_AGE_RANGE

which is equal to: 等于:

sum(QTY_SOLD*SALE) for each CU_AGE_RANGE

How do I get from the current (sum of Category) to desired (avg by Category of sum of Subcategory)? 我如何从当前(类别总和)到所需(平均子类别总和的类别)?

Your count of customers is wrong. 您的客户数量有误。 You are counting the number of sales made, not the number of customers. 您是在计算销售数量,而不是客户数量。 Change to count( DISTINCT c.CUSTOMER_ID ) should solve it. 更改为count( DISTINCT c.CUSTOMER_ID )应该可以解决它。

select c.CU_AGE_RANGE, count( DISTINCT c.CUSTOMER_ID ) as '# of Customers', avg(SumSales) as 'Avg of SumSales', max([Max of SumSales]) as 'Max of SumSales', min([Min of SumSales]) as 'Min of SumSales'
from Customers c
inner join (
    select CUSTOMER_ID, sum(QTY_SOLD * SALES) as SumSales, max(QTY_SOLD*SALES) as 'Max of SumSales', min(QTY_SOLD*SALES) as 'Min of SumSales'
    from Sales
    where (SALES > 0) and (QTY_SOLD > 0) and (COST > 0)
    Group by CUSTOMER_ID
    ) s
on c.CUSTOMER_ID = s.CUSTOMER_ID
group by c.CU_AGE_RANGE

Let's think about the sub-query first: 让我们首先考虑一下子查询:

select Subcategory, sum(Quantity * Price) as 'CalculatedSum'
from ChildTable
group by Subcategory

Each and every record of the resulting relation is representing an aggregation of a Subcategory . 结果关系的每个记录都代表一个Subcategory的集合。 Now, avg(CalculatedSum) should yield the average of the CalculatedSum values. 现在, avg(CalculatedSum)应该产生CalculatedSum值的平均值。 Try to calculate sum(CalculatedSum) instead and see if there is a difference. 尝试改为计算sum(CalculatedSum) ,看看是否存在差异。

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

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