了解JOINS,子查询和聚合函数

时间:2016-03-28 10:40:43

标签: sql join subquery aggregate-functions average

在MYSQL中,我试图理解聚合函数,并尝试在northwind模式中使用一些示例。

表员工有以下描述。

    +-----------------+-------------+------+-----+---------+----------------+
    | Field           | Type        | Null | Key | Default | Extra          |
    +-----------------+-------------+------+-----+---------+----------------+
    | id              | int(11)     | NO   | PRI | NULL    | auto_increment |
    | company         | varchar(50) | YES  | MUL | NULL    |                |
    | last_name       | varchar(50) | YES  | MUL | NULL    |                |
    | first_name      | varchar(50) | YES  | MUL | NULL    |                |
    | email_address   | varchar(50) | YES  |     | NULL    |                |
    | job_title       | varchar(50) | YES  |     | NULL    |                |
    | business_phone  | varchar(25) | YES  |     | NULL    |                |
    | home_phone      | varchar(25) | YES  |     | NULL    |                |
    | mobile_phone    | varchar(25) | YES  |     | NULL    |                |
    | fax_number      | varchar(25) | YES  |     | NULL    |                |
    | address         | longtext    | YES  |     | NULL    |                |
    | city            | varchar(50) | YES  | MUL | NULL    |                |
    | state_province  | varchar(50) | YES  | MUL | NULL    |                |
    | zip_postal_code | varchar(15) | YES  | MUL | NULL    |                |
    | country_region  | varchar(50) | YES  |     | NULL    |                |
    | web_page        | longtext    | YES  |     | NULL    |                |
    | notes           | longtext    | YES  |     | NULL    |                |
    | attachments     | longblob    | YES  |     | NULL    |                |
    +-----------------+-------------+------+-----+---------+----------------+

表中的数据也是

    mysql> select city , first_name,last_name from employees;
    +----------+------------+----------------+
    | city     | first_name | last_name      |
    +----------+------------+----------------+
    | Seattle  | Nancy      | Freehafer      |
    | Bellevue | Andrew     | Cencini        |
    | Redmond  | Jan        | Kotas          |
    | Kirkland | Mariya     | Sergienko      |
    | Seattle  | Steven     | Thorpe         |
    | Redmond  | Michael    | Neipper        |
    | Seattle  | Robert     | Zare           |
    | Redmond  | Laura      | Giussani       |
    | Seattle  | Anne       | Hellung-Larsen |
    +----------+------------+----------------+

我想了解如何找到来自不同城市的平均人数。

直到现在,我有

    mysql> select city,count(city) from employees group by city;
    +----------+-------------+
    | city     | count(city) |
    +----------+-------------+
    | Bellevue |           1 |
    | Kirkland |           1 |
    | Redmond  |           3 |
    | Seattle  |           4 |
    +----------+-------------+

我也有

    SELECT SUM(inner_count_city) from 
    (
            SELECT   city  AS  inner_city, 
            COUNT(*)   AS inner_count_city
            FROM  employees
            GROUP BY inner_city
    ) temp_table;
    +-----------------------+
    | SUM(inner_count_city) |
    +-----------------------+
    |                     9 |
    +-----------------------+

由于以下原因,我正在努力前进。

  • 我无法进行AVG(COUNT(城市)) - 无法做两个聚合功能
  • 我也不确定,如何将它除以城市数量的总和(= 9)。
  • 不确定我是否应该使用联合或联接或子查询。

我正在尝试做类似

的事情
  • 从...选择city,inner_count_city / sum(inner_count_city)。

2 个答案:

答案 0 :(得分:0)

您可以在外部查询中应用AVG聚合函数:

select avg(cnt)
FROM (select count(city) as cnt 
      from employees 
      group by city) as t

要获得每个城市的人口百分比,您可以使用以下查询:

select city, count(city) * 100.0 / total_count as cnt 
from employees 
cross join (select count(*) as total_count from employees) AS t
group by city, total_count

答案 1 :(得分:0)

SQL一次只支持一个聚合;对于多个聚合,您需要多个子查询/ CTE。如果您想要城市中的平均人数,那么您几乎就在那里:

SELECT AVG(inner_count_city * 1.0) from 
FROM (SELECT   city  AS  inner_city, COUNT(*) AS inner_count_city
      FROM  employees
      GROUP BY inner_city
     ) c;

请注意,SQL Server对整数执行整数运算。所以1和2的平均值是1,而不是1.5。因此* 1.0

您也可以在没有子查询的情况下执行此操作:

SELECT COUNT(*) * 1.0 / COUNT(DISTINCT city)
FROM employees;