Mysql查询中的Sum的平均值

chu*_*man 9 mysql subquery aggregate-functions

我在创建查询时遇到了一些问题,该查询给出了总和的平均值.我在stackoverflow中阅读了一些示例,但无法做到.任何人都可以帮我理解如何做到这一点吗?这是我的数据:

Transaction_x0020_Number  Product_x0020_Code  Sales_x0020_Value  Date        Cashier
000356                    350                 24.99              2010-06-04  131    
000356                    726                 32.99              2010-06-04  131    
000357                    350                 24.99              2010-06-04  131    
000358                    350                 24.99              2010-06-04  131    
000358                    360                 24.99              2010-06-04  131    
000770                    703                 69.99              2010-06-04  130    
000771                    726                 32.99              2010-06-04  130    
000772                    1126                5                  2010-06-04  130    
000773                    482                 32.99              2010-06-04  130    
000774                    600                 32.99              2010-06-04  130    
000775                    350                 24.99              2010-06-04  130    
Run Code Online (Sandbox Code Playgroud)

基本上我需要收银员的平均交易价值.我无法运行基本avg,因为它将占用所有行,但每个事务可以有多行.最后我想要:

Cashier| Average|  
131    | 44.31  |(Which comes from the sum divided by 3 transactions not 5 rows)  
130    | 33.15  |  
etc.  
Run Code Online (Sandbox Code Playgroud)

这是我必须对事务进行SUM查询但不知道如何或在何处包含AVG函数的查询.

SELECT `products`.`Transaction_x0020_Number`, 
       Sum(`products`.`Sales_x0020_Value`) AS `SUM of Sales_x0020_Value`, 
       `products`.`Cashier`   
  FROM `products`
GROUP BY `products`.`Transaction_x0020_Number`, `products`.`Date`, `products`.`Cashier`
  HAVING (`products`.`Date` ={d'2010-06-04'})  
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

Vit*_*nko 18

SELECT Cashier,
       Sum(Sales_x0020_Value) / COUNT(DISTINCT Transaction_x0020_Number) AS 'avg'
FROM products 
WHERE Date = {d'2010-06-04'}
GROUP BY Cashier
Run Code Online (Sandbox Code Playgroud)