SQLZOO- select in select Percentages of Germany 写后分析

Percentages of Germany

5.

Germany (population 80 million) has the largest population of the countries in Europe. Austria (population 8.5 million) has 11% of the population of Germany.

Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.

The format should be Name, Percentage for example:

namepercentage
Albania3%
Andorra0%
Austria11%
......

Decimal places

You can use the function ROUND to remove the decimal places.

Percent symbol %

You can use the function CONCAT to add the percentage symbol.

首先

Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.

大框架是用select语句表达出来

SELECT
	name, percentage
FROM 
    world
WHERE  
    continent = 'Europe';
但是表中并无pprecentage,所以用从concat as 函数创建

SELECT
	name,
	concat (A,'%') as percentage
FROM  
    world
WHERE  
    continent = 'Europe';

但是A是什么我们要进行解释,percentage= population of the population of Germany,A 就是这个小数转化为百分数的值,要怎么描述它?.

A-a1,a2,a3
首先 我们要表达出 population of Germany's
a1; population/(SELECT population FROM world WHERE name = 'Germany'

其次 这个值是一个小数,要保留两位小数(看题中的假设答案得)
用round 函数  round(number,digits)
a2; round(a1,2)
a2; round(population/(SELECT population FROM world
		      WHERE name = 'Germany'),2)

最后 要把其转化成另一种数据类型,小数变百分数,使用cast sth as int

a3; cast(a2*100 as int)
cast(round(population/(SELECT population FROM world
		      WHERE name = 'Germany'),2)*100 as int)

结合起来就是

SELECT
	name,
	concat(cast(round(population/(SELECT population FROM world
		      WHERE name = 'Germany'),2)*100 as int),'%') as percentage
FROM 
    world
WHERE 
    continent = 'Europe';

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值