SQL 试图获得计数结果集的平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7737869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to get the average of a count resultset
提问by Xavjer
I have the following SQL:(bitemp)
我有以下 SQL:(bitemp)
SELECT COUNT (*) AS Count
FROM Table T
WHERE (T.Update_time =
(SELECT MAX (B.Update_time )
FROM Table B
WHERE (B.Id = T.Id))
GROUP BY T.Grouping
now I am getting a resultset with a lot of numbers. I want to get the average of this list. At the moment, I am importing the list into excel and use its average function. But there is a AVG function for DB2, but I did not get it to work.
现在我得到了一个包含很多数字的结果集。我想得到这个列表的平均值。目前,我正在将列表导入 excel 并使用其平均功能。但是 DB2 有一个 AVG 函数,但我没有让它工作。
I tried SELECT AVG(COUNT(*))
and also SELECT AVG(*) FROM (theQuery)
.
我SELECT AVG(COUNT(*))
也试过了SELECT AVG(*) FROM (theQuery)
。
回答by DavidEG
You just can put your query as a subquery:
您只需将查询作为子查询:
SELECT avg(count)
FROM
(
SELECT COUNT (*) AS Count
FROM Table T
WHERE T.Update_time =
(SELECT MAX (B.Update_time )
FROM Table B
WHERE (B.Id = T.Id))
GROUP BY T.Grouping
) as counts
Edit: I think this should be the same:
编辑:我认为这应该是一样的:
SELECT count(*) / count(distinct T.Grouping)
FROM Table T
WHERE T.Update_time =
(SELECT MAX (B.Update_time)
FROM Table B
WHERE (B.Id = T.Id))