来自两个表

时间:2018-06-05 03:55:44

标签: mysql sql database case

我无法让案例陈述正确运行 以下是问题 3.从City表中选择名称。从Country表中选择Name。创建一个计算字段' GNPOverPop'计算GNP / City Population并将该字段格式化为6位小数。然后创建另一个计算字段,确定以下内容:

如果城市的人口是:

大于或等于10%的国家人口然后打印10%或更多'进入计算字段值

大于或等于5%的国家人口然后打印 5%或更多'进入计算字段值

大于或等于1%的国家人口然后打印 1%或更多'进入计算字段值

不是上述类别之一,然后打印'少于1%'进入计算字段值

按以下方式订购结果:

首先按GNPOver Pop降序排列 其次是国名 第三个城市名称

您的结果如下所示:

+-----------------------------------+---------------------------------------+------------+-------------------+

| Name                              | name                                  | GNPOverPop | PopPercentageDesc |

+-----------------------------------+---------------------------------------+------------+-------------------+

| Charleston                        | United States                         | 95.558200  | .1% or more       |

| Carson                            | United States                         | 95.530312  | .1% or more       |

| Odessa                            | United States                         | 95.312063  | .1% or more       |

| Elgin                             | United States                         | 95.189469  | .1% or more       |

| Kenosha                           | United States                         | 95.147965  | .1% or more       |

| Fall River                        | United States                         | 93.983767  | .1% or more       |

| Santa Monica                      | United States                         | 93.437925  | .1% or more       |

| Cary                              | United States                         | 93.305779  | .1% or more       |

| Boulder                           | United States                         | 93.280212  | .1% or more       |

| Visalia                           | United States                         | 92.747543  | .1% or more       |

| San Mateo                         | United States                         | 92.710160  | .1% or more       |

| Arden-Arcade                      | United States                         | 92.467405  | .1% or more       |

| Fairfield                         | United States                         | 92.250911  | .1% or more       |

| Gainesville                       | United States                         | 92.215926  | .1% or more       |

| Compton                           | United States                         | 91.646925  | .1% or more       |

| Billings                          | United States                         | 91.524713  | .1% or more       |

| Roanoke                           | United States                         | 91.162955  | .1% or more       |

| Brockton                          | United States                         | 90.874825  | .1% or more       |

这是我的查询

SELECT City.name, Country.Name, FORMAT((Country.GNP/City.Population),6) AS 
'GNPOverPop', 
case Country.Population 
WHEN City.Population>=0.1 THEN '10% OR MORE' 
WHEN City.Population>=0.05 THEN '5% OR MORE' 
WHEN City.Population>=0.01 THEN '1% OR MORE' 
ELSE 'Less than 1%' END AS 'PopPercentageDesc' 
FROM City JOIN Country ON City.Country = Country.Code 
ORDER BY GNPOverPop DESC, Country.Name, City.name

**我的案例陈述结果只返回其他"少于1%"为了一切。提前致谢。 **

1 个答案:

答案 0 :(得分:0)

这是我运行的查询,行为是预期的。

查询:

create table city (name varchar(20),popula decimal (5,2), GNP decimal(5,2))
  insert into city 
  values
  ('HYD',0.1,100),
  ('BOM',0.06,30),
  ('CHN',0.02,40),
  ('DEL' ,0.2,50);


  select name,case when popula >= 0.1 then '10% or more'
    when popula >= 0.05 and popula < 0.1 then '5% or more'
    when popula >= 0.01 and popula <0.05 then '1% or more'
    else 'less than 1%' end as PopPercentageDESC
  from city

输出:

name    PopPercentageDESC
HYD    10% or more
BOM    5% or more
CHN    1% or more
DEL    10% or more