尝试获取计数结果集的平均值

发布于 2024-12-09 16:18:25 字数 414 浏览 0 评论 0原文

我有以下 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

现在我得到一个包含很多数字的结果集。我想得到这个列表的平均值。目前,我正在将列表导入Excel并使用其平均功能。但是DB2有一个AVG功能,但我没有让它工作。

我尝试了SELECT AVG(COUNT(*))SELECT AVG(*) FROM (theQuery)

I have the following 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.

I tried SELECT AVG(COUNT(*)) and also SELECT AVG(*) FROM (theQuery).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫之都 2024-12-16 16:18:25

您只需将查询作为子查询:

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

编辑:我认为这应该是相同的:

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))

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文