简体   繁体   English

SQL总和除以结果数

[英]SQL sum and divide by number of results

Can't get it to work... this is what I got so far: 无法正常工作...这是我到目前为止的结果:

SELECT CardCode, (sum(DocRate) DocRate/DocRate.Length)
FROM OPCH
WHERE DocRate > 1
  AND DocStatus = 'O'
  AND DocDate >= '20140101'
GROUP BY CardCode

Trying to get the sum of DocRate (which is diffrent each row) and divide it with number of rows (to get an average). 尝试获取DocRate的总和(每行各不相同),然后将其除以行数(以获取平均值)。

This isn't working. 这不起作用。

Well easy enough, found the solution. 好容易,找到了解决方案。
Using AVG() . 使用AVG()

My code: 我的代码:

SELECT CardCode,
       avg(DocRate) DocRate
FROM OPCH
WHERE DocRate > 1
  AND DocStatus = 'O'
  AND DocDate >= '20140101'
GROUP BY CardCode

Try using sub query. 尝试使用子查询。

SELECT CardCode, (DocRate/length)
FROM
  (SELECT CardCode,sum(DocRate) DocRate, sum (DocRate.Length) length
FROM OPCH
WHERE DocRate > 1
  AND DocStatus = 'O'
  AND DocDate >= '20140101'
GROUP BY CardCode) doc
GROUP BY CardCode

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

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