Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL统计查询_Sql_Database_Statistics - Fatal编程技术网

SQL统计查询

SQL统计查询,sql,database,statistics,Sql,Database,Statistics,我想收集一些统计数据,我想知道是否可以通过DB查询直接获取一些最小值、最大值和平均值 简单地说,我有这样的想法: Person | Account __________________ Person1 | Account1 Person1 | Account2 Person1 | Account3 Person2 | Account4 Person2 | Account5 .................. 我想找出一个人拥有的最大、最小和平均帐户数。对于SQL查询,这可能吗 select

我想收集一些统计数据,我想知道是否可以通过DB查询直接获取一些最小值、最大值和平均值

简单地说,我有这样的想法:

Person  | Account
__________________
Person1 | Account1
Person1 | Account2
Person1 | Account3
Person2 | Account4
Person2 | Account5
..................
我想找出一个人拥有的最大、最小和平均帐户数。对于SQL查询,这可能吗

select
    min(accountcount),
    avg(accountcount),
    max(accountcount)
from
(
    select 
        person, count(*) as accountcount
    from 
        yourtable
    group by person
) v
AVG将返回平均值,而不是任何其他类型的平均值

是的,您可以使用函数来执行这种类型的操作:

马克斯 闵 平均值


您必须按人员分组,然后再对子查询,如下所示:

SELECT MIN(PersonCount), MAX(PersonCount), AVG(PersonCount) FROM 
(SELECT Person, COUNT(*) AS PersonCount FROM PersonAccounts GROUP BY Person)

您需要该子查询的别名:
SELECT MIN(PersonCount), MAX(PersonCount), AVG(PersonCount) FROM 
(SELECT Person, COUNT(*) AS PersonCount FROM PersonAccounts GROUP BY Person)