将字母数字字符串转换为Redshift中的整数

huangapple go评论76阅读模式
英文:

Convert alphanumeric string to integer in Redshift

问题

我有一个包含多列主键的表。出于ETL作业的目的,我需要将所有主键列连接到一个单独的列中进行去重。

假设我的组合主键是:

jesse_20230431_low
james_20230431_high

有没有办法将其转换为保持唯一性的数字?想象一下,每个字符都可以用一个数字表示,是这样的吧。

英文:

I have a table that contains a multi-column primary key. For the purpose of ETL job, I need to concatenate all primary key columns into a single column for de-duplication.

Let's say that my combined primary key are:

jesse_20230431_low
james_20230431_high

Is there anyway for me to convert that into a number that retains its uniqueness? Figure every character can be represented by a number no? Something like that.

答案1

得分: 1

你可以使用每个字符的ascii()值,但字符串会变得很大。

查询:

with cte as
(
  SELECT ascii(unnest(string_to_array('jesse_20230431_low james_20230431_high', NULL))) col
)
select string_agg(cast(col as varchar), '') from cte

输出:

string_agg
10610111511510195504850514852514995108111119321069710910111595504850514852514995104105103104

fiddle

英文:

You can use ascii() value of each character but the string will be huge.

Query:

with cte as
  (
    SELECT ascii( unnest( string_to_array('jesse_20230431_low james_20230431_high', NULL) ) ) col 
  )
select string_agg(cast(col as varchar),'') from cte

Output:

string_agg
10610111511510195504850514852514995108111119321069710910111595504850514852514995104105103104

fiddle

huangapple
  • 本文由 发表于 2023年5月11日 01:33:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定