【问题标题】:Need help writing a SQL query需要帮助编写 SQL 查询
【发布时间】:2011-03-14 05:57:31
【问题描述】:

我有以下表格。

客户表
客户ID |姓氏 |名字 |城市_ID |区域_ID | Country_ID

Country_table
Country_ID |国家

Region_table
Region_ID |地区 | Country_ID

City_table
City_ID |城市 |区域_ID | Country_ID

我需要按城市计算客户数量,即所有国家、国家的所有地区、该地区的所有城市,我需要得到客户数量。每个城市的客户。

例如,我编写了以下 SQL 来获取否。每个国家/地区的客户:

SELECT a.country_id , b.country,count(*) 
FROM Customer_Table a INNER JOIN Country_Table b
ON a.country_id = b.country_id
GROUP BY b.country , b.country_id ;

并获得否。特定国家/地区每个地区的客户数量:

SELECT a.region_id , b.region , count(*)
FROM Customers_table a INNER JOIN Region_Table b
ON a.region_id = b.region_id
WHERE a.country_id ='USA' AND b.country_id ='USA'
GROUP BY region_id , country ;

我需要找到的是城市方面的客户数量,即,对于所有国家,对于国家的所有地区,对于该地区的所有城市,我需要得到编号。每个城市的客户数量,例如

Country | Region  |       City     | no.of customers
  USA   |   CA    |  San Francisco |    53
  USA   |   CA    |  Los Angeles   |    45
  USA   |   CA    |  Sacramento    |    16
  USA   |   WA    |  Seattle       |    46
  USA   |   WA    |  Olympia       |    9
  UK    | England |  London        |    89
  UK    | England |  Nottingham    |    71
  UK    | Wales   |  Cardiff       |    69
  UK    | Wales   |  Newport       |    23

  ..................and so on for other countries.

【问题讨论】:

    标签: sql mysql sql-server database oracle


    【解决方案1】:

    对于 SQL Server:

    SELECT Country, Region, City, COUNT(Customer_ID) as [no.of customers]
    FROM Country_Table
    INNER JOIN Region_Table ON Country_Table.Country_ID = Region_Table.Country_ID
    INNER JOIN City_Table ON City_Table.Region_ID = Region_Table.Region_ID
    INNER JOIN Customer_Table ON Customer_Table.City_ID = City_Table.City_ID
    GROUP BY Country, Region, City;
    

    【讨论】:

    • 不需要查询中的两个AND 子句。
    【解决方案2】:

    您的表格有多余的信息

    由于它们是链接的,因此每个表只需要引用其直接父级,而不是层次结构中的所有父级..

    所以客户只需要参考城市,城市只需要地区,地区只需要国家

    SELECT
        Country_Table.Country,
        Region_Table.Region,
        City_Table.City,
        Count(Customer_Id) AS Customers
    FROM
        Country_Table
        INNER JOIN Region_Table ON Region_Table.Country_Id = Country_Table.Country_Id
        INNER JOIN City_Table ON City_Table.Region_Id = Region_Table.Region_Id
        INNER JOIN Customer_Table ON Customer_Table.City_Id = City_Table.City_Id
    GROUP BY
        Country_Table.Country,
        Region_Table.Region,
        City_Table.City
    

    【讨论】:

    • @Gaby,所以您是说客户表不需要区域和国家/地区字段?
    • 如果 customer 表在 city 表中,则不需要它们。如果 region 表有,那么严格来说 city 表不需要国家。如果你像这样保留冗余数据,你的数据库迟早会包含矛盾。
    • @Zaki,是的,因为可以从城市表中找到区域,并且可以从区域中找到国家.. 这是因为它的客户只能属于一个城市,一个城市只能属于一个地区和一个地区到一个国家..所以每个表只需要引用父级而不需要其他..
    • 这里还有一个问题,我在这里的答案中运行了一个 sql 并且它花费了很长时间,我的意思是它已经超过 6-7 分钟才能得到结果。我对服务器上的 Oracle 11g 数据库和 customer_table 使用 sql developer 具有约 80,000 行。什么可能需要这么长时间的查询?
    • @zaki,您应该在各自的表中对城市、地区和国家字段进行索引。
    【解决方案3】:

    试试这个:

    select Country,Region,City,count(Customer_ID)
    from Customer_table as cust
    inner join  Country_table on Country_ID=cust.Country_ID
    inner join Region_table on Region_ID=cust.Region_ID
    inner join Region_table on City_ID=cust.City_ID
    group by country,Region,City
    

    【讨论】:

      猜你喜欢
      • 2019-10-11
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多