我在vertica db中有以下表格:
+-----+-------+-------+-------+-------+
| 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生成apriori算法,为了生成新的候选人,我想使用F_k-1
加入,在我的理解中需要按照上面的要求进行排序。
感谢您的帮助。
答案 0 :(得分:3)
我无法访问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 |
+-----+-------+-------+-------+-------+