Ms Access: How can I take the average of a column but ignore the rows where another column is 0?

三世轮回 提交于 2020-01-15 08:47:11

问题


I have 2 columns, qty_req and qty_issued in a report. I need to find the average of the values in the qty_issued column. The problem is that sometimes the corresponding value of qty_req is 0. I need to take the average of the qty_issued column for only the rows where qty_req is NOT 0. How do I do this?

Spun off from my other question here: MS Access: How can I average a list of quantities on a report where the quantities are not zero?


回答1:


If you want to do that in the Control Source of a text box on your report, you can take advantage of the fact that Avg() ignores Null values.

So, when qty_req <> 0, include qty_issued among the values which are averaged. Otherwise use Null instead of the qty_issued value.

=Avg(IIf(qty_req <> 0, qty_issued, Null))

If you want to do it in a query instead ...

SELECT Avg(IIf(qty_req <> 0, qty_issued, Null)) FROM YourTable;



回答2:


You would set the criteria in the query to not look at the qty_req that is 0:

SELECT Avg(MyTable.qty_issued) AS Avg_Issued
FROM MyTable
GROUP BY MyTable.qty_req
HAVING MyTable.qty_req <> 0;


来源:https://stackoverflow.com/questions/38747275/ms-access-how-can-i-take-the-average-of-a-column-but-ignore-the-rows-where-anot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!