在红移中对逗号分隔值

发布于 2025-01-25 17:10:23 字数 802 浏览 3 评论 0原文

我的红移中有一个comma分离值的列/字段。

My_field
Apple,香蕉,糖果
糖果,香蕉
香蕉,苹果

和我想将它们变成

my_field_sorted
苹果,香蕉,糖果
香蕉,糖果
苹果,香蕉,

有什么简单的方法可以使用本机SQL/Redshift Syntax做到这一点吗? 我必须求助于UDFS吗?

我没有创建UDFS的经验 - 有人有类似的UDF可以共享吗?

还是有人可以帮助将此功能变成UDF?

def sort_comma_sep_values(x):
  if type(x) is str:
    list_of_x = x.split(',')
    list_of_x = list(dict.fromkeys(list_of_x))
    list_of_x = sorted(list_of_x)
    output = ','.join(list_of_x)
    return output
  else:
    return None

I have a column/field in Redshift with comma separated values.

my_field
apple,banana,candy
candy,banana
banana,apple

And I want to turn these into

my_field_sorted
apple,banana,candy
banana,candy
apple,banana

Is there an easy way to do this with native SQL/Redshift syntax?
Do I have to resort to UDFs?

I don't have any experience creating UDFs - does anyone have a similar UDFs that they can share?

Or can someone help turn this function into an UDF?

def sort_comma_sep_values(x):
  if type(x) is str:
    list_of_x = x.split(',')
    list_of_x = list(dict.fromkeys(list_of_x))
    list_of_x = sorted(list_of_x)
    output = ','.join(list_of_x)
    return output
  else:
    return None

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文