Teradata 将 varchar 列行值转换为 Columns
Teradata convert varchar column row values to Columns
我有以下数据:
create volatile table current_data(
id integer
, rownumber integer
, text_value varchar(10)
) on commit preserve rows;
insert into current_data values(1,1,'Text 1');
insert into current_data values(1,2,'Text 2');
insert into current_data values(1,3,'Text 3');
insert into current_data values(2,1,'Text 1');
insert into current_data values(2,2,'Text 2');
如何将 current_data
转换为我的 desired_data
?
create volatile table desired_data(
id integer
, text_value1 varchar(10)
, text_value2 varchar(10)
, text_value3 varchar(10)
)
on commit preserve rows;
insert into desired_data values(1,'Text 1','Text 2','Text 3');
insert into desired_data values(2,'Text 1','Text 2',null);
*** 注意,我会包含图片结果,但我没有适当的访问权限。
您可以使用条件聚合:
select id,
max(case when rownumber = 1 then text_value end) as text_value1,
max(case when rownumber = 2 then text_value end) as text_value2,
max(case when rownumber = 3 then text_value end) as text_value3
from current_data
group by id;
我有以下数据:
create volatile table current_data(
id integer
, rownumber integer
, text_value varchar(10)
) on commit preserve rows;
insert into current_data values(1,1,'Text 1');
insert into current_data values(1,2,'Text 2');
insert into current_data values(1,3,'Text 3');
insert into current_data values(2,1,'Text 1');
insert into current_data values(2,2,'Text 2');
如何将 current_data
转换为我的 desired_data
?
create volatile table desired_data(
id integer
, text_value1 varchar(10)
, text_value2 varchar(10)
, text_value3 varchar(10)
)
on commit preserve rows;
insert into desired_data values(1,'Text 1','Text 2','Text 3');
insert into desired_data values(2,'Text 1','Text 2',null);
*** 注意,我会包含图片结果,但我没有适当的访问权限。
您可以使用条件聚合:
select id,
max(case when rownumber = 1 then text_value end) as text_value1,
max(case when rownumber = 2 then text_value end) as text_value2,
max(case when rownumber = 3 then text_value end) as text_value3
from current_data
group by id;