从给定的输入生成最高数字并使用 hive 将 0 替换为 9

发布于 2025-01-11 23:02:44 字数 509 浏览 2 评论 0原文

有人可以帮助/建议我应该如何处理配置单元中的以下情况。

我有一列包含一些值,其中一些数字后有 0(6 位数字后),我需要将所有这些 0 替换为 9。如果我在 5 位数字后有 0,那么我需要在开头包含 0,然后再次需要替换 6 位数字后面的 0。PFB 一些样本记录和预期输出。

       Input                    output
    1234560000000             1234569999999
     123450000000             0123459999999
      12340000000             0012349999999
       1230000000             0001239999999

所以这里基本上我需要从右到左检查。 1234560000000 在这里,我将从右(0)开始检查,一旦找到任何数字,我将用 9 替换所有 0,如果除 0 之外的数字计数小于 6,则将在开头添加 0。

请建议

Can Someone Help/suggest me how should I handle below scenario in hive.

I have one column which contain some values in which I have 0's after some digits(after 6 digits) I need to replace all these 0 by 9. and if i have 0's after 5 digits then i need to include 0 at the starting and then again need to replace 0's which comes after 6 digits.PFB some sample records and expected output.

       Input                    output
    1234560000000             1234569999999
     123450000000             0123459999999
      12340000000             0012349999999
       1230000000             0001239999999

so here basically I need to check from right to left.ie. 1234560000000
here I will start checking from right (0) and as soon as find any digit i will replace all the 0's by 9 and if digit count other then 0 is less than 6 then will add 0 in the beginning.

kindly suggest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何必那么矫情 2025-01-18 23:02:44

将字符串拆分为两部分:末尾的数字和其他所有内容,用 9 替换零,连接。

演示:

with mytable as (
select '1234560000000' as input union all
select '123450000000' union all
select '12340000000' union all
select '1230000000'   
)

select lpad(concat(splitted[0], translate(splitted[1],'0','9')),13,0)
from
(
select split(regexp_replace(input,'(\\d*?)(0+)

结果

"1234569999999"
"0123459999999"
"0012349999999"
"0001239999999"
,'$1|$2'),'\\|') splitted from mytable )s

结果

Split string on two parts: digits at the end and everything else, replace zeroes with 9, concatenate.

Demo:

with mytable as (
select '1234560000000' as input union all
select '123450000000' union all
select '12340000000' union all
select '1230000000'   
)

select lpad(concat(splitted[0], translate(splitted[1],'0','9')),13,0)
from
(
select split(regexp_replace(input,'(\\d*?)(0+)

Result

"1234569999999"
"0123459999999"
"0012349999999"
"0001239999999"
,'$1|$2'),'\\|') splitted from mytable )s

Result

原野 2025-01-18 23:02:44

我从未使用过 hive,但它看起来具有我在其他数据库中使用的所有功能,

我们如何将所有 0 替换为空格,然后将空格去掉right 和 rpad 用 '9' 达到原始长度,然后 lpad 用 '0' 最多 13,然后替换 任何空格(在中间,例如如果我们要转换101010000 到 000101019999) 返回“0”

replace(
  lpad(
    rpad(
      rtrim(
        replace(num, '0', ' ')
      ),
      length(num),
      '9'
    ),
    13,
    '0'
  ),
  ' ',
  '0'
)

如前所述,从未使用过 hive,因此这里可能需要解决一些小语法问题 - 如果 hive 没有通过自动将数字转换为字符串来提供数字的长度,例如,你需要插入一些东西来进行显式转换。您也可以对数字进行 log10。

我不知道有任何小提琴网站提供蜂巢作为测试器。

I've never used hive but it looks to have all the functions I would use in some other DB

How about we replace all the 0 to spaces, rtrim the spaces off the right and rpad with '9' out to the original length, then lpad up to 13 with '0' and then replace any spaces (in the middle, eg if we are converting 101010000 to 000101019999) back to '0'

replace(
  lpad(
    rpad(
      rtrim(
        replace(num, '0', ' ')
      ),
      length(num),
      '9'
    ),
    13,
    '0'
  ),
  ' ',
  '0'
)

As noted, never used hive so there might be some minor syntax things to resolve here - if hive doesn't give you the length of a number by auto converting it to a string, for example, you'll need to stick something in for doing an explicit convert. You could alternatively do the log10 of the number.

I don't know of any fiddle sites that offer hive as a tester..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文