【问题标题】:How can I do natural sort in Snowflake?如何在雪花中进行自然排序?
【发布时间】:2020-02-19 19:08:52
【问题描述】:
我正在尝试使用几种可能的结构对 Jira 故事进行排序:3 到 5 个字符后跟破折号,后跟 3 或 4 位数字,例如XXX-###、XXX-####、XXXX-###等
【问题讨论】:
标签:
snowflake-cloud-data-platform
natural-sort
【解决方案1】:
我不太清楚你所说的自然排序是什么意思,但你可以按正则表达式排序。例如,仅获取 JiraID 字符串的编号。
--This Returns '123'
select REGEXP_SUBSTR( 'xx-123' , '\\d+');
--This Returns 'xx'
select REGEXP_SUBSTR( 'xx-123' , '\\w+');
--So this would sort by just the number in a Jira number column based on the number part
... order by REGEXP_SUBSTR( JiraID , '\\d+')::int;
--This would order first by IDs with the same string, and then order by Number
... order by REGEXP_SUBSTR( JiraID , '\\w+'), REGEXP_SUBSTR( JiraID , '\\d+')::int;
如果您提供具有所需输出顺序的示例输入,则可能会出现更复杂的示例