postgresql – 圆号UP到10的倍数

前端之家收集整理的这篇文章主要介绍了postgresql – 圆号UP到10的倍数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在Postgresql中轻松地将数字UP转换为10的倍数?

例:

  1. In Out
  2. 100 --> 100
  3. 111 --> 120
  4. 123 --> 130

样品数据:

  1. create table sample(mynumber numeric);
  2.  
  3. insert into sample values (100);
  4. insert into sample values (111);
  5. insert into sample values (123);

我可以用:

  1. select
  2. mynumber,case
  3. when mynumber = round(mynumber,-1) then mynumber
  4. else round(mynumber,-1) + 10 end as result
  5. from
  6. sample;

这样做很好,但看起来很丑陋.有更简单的做法吗?

你可以找到sqlFiddle here

  1. select ceil(a::numeric / 10) * 10
  2. from (values (100),(111),(123)) s(a);
  3. ?column?
  4. ----------
  5. 100
  6. 120
  7. 130

猜你在找的Postgre SQL相关文章