数据库 之 round函数

用法

ROUND() 函数用于把数值字段舍入为指定的小数位数,主要有两种形式:

  1. ROUND(x,d) ,x 指要处理的数,d 是指保留几位小数,其中 d 可以为负数,当 d 为 -3 时表示保留整数到千位
  2. ROUND(x) ,其实就是ROUND(x,0),也就是默认d为0

例题

以SQLZoo上的两道题为例子:

1. 题目一

 Show the name and population in millions and the GDP in billions for the countries of the continent ‘South America’. Use the ROUND function to show the values to two decimal places.
 For South America show population in millions and GDP in billions both to 2 decimal places.
 该题目很简单,就是将人口数单位转变为百万级,将GDP转变为十亿级别,并且保留两位小数。答案如下:

select name,round(population/1000000,2),round(gdp/1000000000,2) 
from world 
where continent = 'South America'

2. 题目二

 Show the name and per-capita GDP for those countries with a GDP of at least one trillion (1000000000000; that is 12 zeros). Round this value to the nearest 1000.
 Show per-capita GDP for the trillion dollar countries to the nearest $1000.
 该题是求人均GDP,并且将数字保留到千位,让d = -3,答案如下:

select name,round(gdp/population,-3) 
from world 
where gdp > 1000000000000

你可能感兴趣的:(数据库,big,data)