需要获得平均计数

发布于 2025-01-19 18:26:32 字数 182 浏览 3 评论 0原文

我想获得客户拥有的产品=A 的平均值。假设 6 个客户的内部选择返回 1,2,1,4,4,4

我希望看到结果为 4,这意味着客户可以拥有的平均产品数量是 4

有人可以确认以下内容吗? 例如

选择 avg(count) 从 ( 选择 count(*) 作为计数 从表1 其中产品 = A 按客户分组) 作为计数

I would like to get average of product=A that a client have. Say inner select return 1,2,1,4,4,4 for 6 clients

I would like to see result as 4 which means the avg product count a client can have is 4

Can somebody please confirm the following.
E.g

Select avg(count)
From (
Select count(*) as count
From Table1
Where product = A
Group by client)
as counts

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

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

发布评论

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

评论(1

美人如玉 2025-01-26 18:26:32

拥有示例数据对于获得帮助很重要。仍然很难确定您的数据外观。假设它看起来像这样:

    create table table1 (
      client varchar(10), 
      product varchar(10)
     );
     
    insert into table1 values
    ('xxx', 'A'),
    ('bbb', 'A'), 
    ('bbb', 'A'), 
    ('ccc', 'A'), 
    ('ddd', 'A'),
    ('ddd', 'A'),
    ('ddd', 'A'),
    ('ddd', 'A'),
    ('tt', 'A'), 
    ('tt', 'A'), 
    ('tt', 'A'), 
    ('tt', 'A'), 
    ('bdad', 'A'), 
    ('bdad', 'A'), 
    ('bdad', 'A'), 
    ('bdad', 'A');

我无法访问DB2数据库,但是此查询适用于大多数DBMS类型。您可能需要调整以适合DB2。

    select purchased as most_common_value
    from (
      select client, count(*) as purchased
      from table1
      where product = 'A'
      group by client
      )z
    group by purchased
    order by count(client) desc
    limit 1

查询的输出是:

    most_common_value
    4

Having sample data is important to getting assistance. It's still difficult to determine how your data looks. Let's assume it looks like this:

    create table table1 (
      client varchar(10), 
      product varchar(10)
     );
     
    insert into table1 values
    ('xxx', 'A'),
    ('bbb', 'A'), 
    ('bbb', 'A'), 
    ('ccc', 'A'), 
    ('ddd', 'A'),
    ('ddd', 'A'),
    ('ddd', 'A'),
    ('ddd', 'A'),
    ('tt', 'A'), 
    ('tt', 'A'), 
    ('tt', 'A'), 
    ('tt', 'A'), 
    ('bdad', 'A'), 
    ('bdad', 'A'), 
    ('bdad', 'A'), 
    ('bdad', 'A');

I don't have access to a DB2 database, but this query works for most dbms types. You may need to tweak to fit DB2.

    select purchased as most_common_value
    from (
      select client, count(*) as purchased
      from table1
      where product = 'A'
      group by client
      )z
    group by purchased
    order by count(client) desc
    limit 1

Output of query is:

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