Vertica:如何按某种顺序连接值?
Vertica: how can you concate values by some order?
假设你有列
ID | A | B | C
1 | 3 | 1 | 2
2 | 5 | 9 | 1
3 | 1 | 2 | 3
并且您希望将列连接起来,使最终结果看起来像
ID | ABC_value_DESC | ABC_value_DESC_colnames
1 | 3,2,1 | A,C,B
2 | 9,5,1 | B,A,C
3 | 3,2,1 | C,B,A
您想在新列 ABC_value_DESC
中按降序获取 col 值,然后 return 在新列 ABC_value_DESC_colnames
.[=15= 中相应的列名]
如何在 Vertica 9 中将多个列的值按降序连接到一个新列中,return 列名称按值顺序(不是名称顺序)连接?
Ps。我已经尝试过 Listagg -function 但错误导致排序未实现并且在尝试 Vertica 的建议时 here giving false result and even bugs with alternative here.
你可以用一个讨厌的 case
表达式来做到这一点。对于三列来说还不错:
select t.*,
(gr || ',' ||
(case when a not in (le, gr) then a
when b not in (le, br) then b
else c
end) || ',' ||
le
),
((case gr when a then 'a' when b then 'b' else 'c' end) || ',' ||
(case when a not in (gr, le) then 'a'
when b not in (gr, le) then 'b'
else 'c'
end) || ',' ||
(case le when a then 'a' when b then 'b' else 'c' end)
)
from (select t.*, greatest(a, b, c) as gr, least(a, b, c) as le
from t
) t;
此特定版本假设没有重复或 NULL
值,尽管可以为此目的采用。
假设你有列
ID | A | B | C
1 | 3 | 1 | 2
2 | 5 | 9 | 1
3 | 1 | 2 | 3
并且您希望将列连接起来,使最终结果看起来像
ID | ABC_value_DESC | ABC_value_DESC_colnames
1 | 3,2,1 | A,C,B
2 | 9,5,1 | B,A,C
3 | 3,2,1 | C,B,A
您想在新列 ABC_value_DESC
中按降序获取 col 值,然后 return 在新列 ABC_value_DESC_colnames
.[=15= 中相应的列名]
如何在 Vertica 9 中将多个列的值按降序连接到一个新列中,return 列名称按值顺序(不是名称顺序)连接?
Ps。我已经尝试过 Listagg -function 但错误导致排序未实现并且在尝试 Vertica 的建议时 here giving false result and even bugs with alternative here.
你可以用一个讨厌的 case
表达式来做到这一点。对于三列来说还不错:
select t.*,
(gr || ',' ||
(case when a not in (le, gr) then a
when b not in (le, br) then b
else c
end) || ',' ||
le
),
((case gr when a then 'a' when b then 'b' else 'c' end) || ',' ||
(case when a not in (gr, le) then 'a'
when b not in (gr, le) then 'b'
else 'c'
end) || ',' ||
(case le when a then 'a' when b then 'b' else 'c' end)
)
from (select t.*, greatest(a, b, c) as gr, least(a, b, c) as le
from t
) t;
此特定版本假设没有重复或 NULL
值,尽管可以为此目的采用。