Sql:按字母数字顺序对行条目进行排序

Sql: Sorting row entries alphanumerically

我在 vertica 数据库中有以下 table:

+-----+-------+-------+-------+-------+
| Tid | Item1 | Item2 | Item3 | Item4 |
+-----+-------+-------+-------+-------+
|   1 | A     | B     | C     | D     |
|   2 | A     | E     | B     | D     |
|   3 | D     | A     | 5     | C     |
|   4 | B     | 1     | E     | A     |
+-----+-------+-------+-------+-------+

我的目标是按字母数字对行进行排序,所有条目都是字符串。结果看起来像这样:

+-----+-------+-------+-------+-------+
| Tid | Item1 | Item2 | Item3 | Item4 |
+-----+-------+-------+-------+-------+
|   1 | A     | B     | C     | D     |
|   2 | A     | B     | D     | E     |
|   3 | 5     | A     | C     | D     |
|   4 | 1     | A     | B     | E     |
+-----+-------+-------+-------+-------+

用法:我正在使用 sql 生成先验算法,为了生成新的候选对象,我想使用 F_k-1 连接,据我所知,这需要像上面要求的那样进行排序。

感谢您的帮助。

我无权访问 Vertica,但这应该可以完成工作(尽管我不能保证这是 Vertica 最有效的方式/最干净的代码)

select      Tid
           ,min (case rn when 1 then item end)  as Item1
           ,min (case rn when 2 then item end)  as Item2
           ,min (case rn when 3 then item end)  as Item3
           ,min (case rn when 4 then item end)  as Item4

from       (select      Tid,item
                       ,row_number () over (partition by Tid order by item) as rn

            from       (            select Tid ,Item1 as item from t
                        union all   select Tid ,Item2         from t
                        union all   select Tid ,Item3         from t
                        union all   select Tid ,Item4         from t
                        ) t
            ) t

group by    Tid

+-----+-------+-------+-------+-------+
| tid | item1 | item2 | item3 | item4 |
+-----+-------+-------+-------+-------+
| 1   | A     | B     | C     | D     |
+-----+-------+-------+-------+-------+
| 2   | A     | B     | D     | E     |
+-----+-------+-------+-------+-------+
| 3   | 5     | A     | C     | D     |
+-----+-------+-------+-------+-------+
| 4   | 1     | A     | B     | E     |
+-----+-------+-------+-------+-------+