我们需要以这样的缩写方式获取一些非常大的 numbers 和 display :
2113546998.37 --> 21.37B
15481063.31 --> 15.31M
等等。我不认为 Oracle 有这样做的方法。希望得到一些帮助。
回答1
您可以使用 log 和 power 来操作和解释 value;将它四舍五入到最接近的“大数”括号的小数位:
round(your_number / power(10, 3 * floor(log(10, your_number) / 3)), 2)
然后要附加字母,例如:
case 3 * floor(log(10, your_number) / 3)
when 0 then null when 3 then 'K' when 6 then 'M'
when 9 then 'B' when 12 then 'T' when 15 then 'Q'
end
依此类推,但如果你变得比这更大,你将不得不决定如何区分千万亿和五千万亿。
使用一些扩展示例数据,完整查询:
select your_number,
round(your_number / power(10, 3 * floor(log(10, your_number) / 3)), 2)
||
case 3 * floor(log(10, your_number) / 3)
when 0 then null when 3 then 'K' when 6 then 'M'
when 9 then 'B' when 12 then 'T' when 15 then 'Q'
else 'x'
end as result
from your_table
order by your_number
得到
YOUR_NUMBER | RESULT |
---|---|
123.456789 | 123.46 |
1234.56789 | 1.23K |
12345.6789 | 12.35K |
123456.789 | 123.46K |
1234567.89 | 1.23M |
15481063.31 | 15.48M |
123456789 | 123.46M |
2113546998.37 | 2.11B |
123456789123 | 123.46B |
123456789123456 | 123.46T |
所以你的两个原始 values 得到 2.11B 和 15.48M,而不是你的问题显示的 21.37B 和 15.31M - 但正如评论中指出的那样,只保持精度的两个极端是没有意义的。这样做是可能的,当然 - floor 而不是 round,并附加原始小数部分 - 但它似乎不太可能是你真正的意思,我假设 21 vs 2 和小数部分都是将问题放在一起的错误.
不过,您可能不想将它应用于较小的 numbers - 'K' 可能不太常见? - 如果是这样,您可以使用另一个 case 表达式来决定。例如:
select your_number,
case
when log(10, your_number) < 6
then to_char(round(your_number, 2))
else
round(your_number / power(10, 3 * floor(log(10, your_number) / 3)), 2)
||
case 3 * floor(log(10, your_number) / 3)
when 6 then 'M' when 9 then 'B' when 12 then 'T' when 15 then 'Q'
else 'x'
end
end as result
from your_table
order by your_number
YOUR_NUMBER | RESULT |
---|---|
123.456789 | 123.46 |
1234.56789 | 1234.57 |
12345.6789 | 12345.68 |
123456.789 | 123456.79 |
1234567.89 | 1.23M |
15481063.31 | 15.48M |
123456789 | 123.46M |
2113546998.37 | 2.11B |
123456789123 | 123.46B |
123456789123456 | 123.46T |
无论哪种方式,您都可以轻松地将逻辑放入函数中。
https://dbfiddle.uk/?rdbms=oracle_21&fiddle=00af7ae30fcfd6d35f5d7ce9514329d7
我只看过正的、非零的 numbers;如果您需要处理零或负 numbers,那么它需要https://dbfiddle.uk/?rdbms=oracle_21&fiddle=c482a8294f78136b4c6c172c83e28764。