Trafodion 行列转换

Trafodion中提供一个函数叫做group_concat,用于把多列转成一行,具体语法请参考官方手册 http://trafodion.incubator.apache.org/docs/sql_reference/index.html#group_concat_function

下面是自己作的一个小试验测试group_concat基本功能,

--Test group_concat function

drop table if exists employee;
create table employee (
empno int not null,
attr char(20) not null,
value char(20),
primary key (empno, attr)
);

insert into employee values(1,'name','Kevin');
insert into employee values(2,'name','Eason');
insert into employee values(3,'name','Hank');
insert into employee values(1,'address','Address A');
insert into employee values(2,'address','Address B');
insert into employee values(3,'address','Address C');
insert into employee values(1,'title','Junior Engineer');
insert into employee values(2,'title','Senior Engineer');
insert into employee values(3,'title','Architecture');

cqd mode_special_4 'on';

select * from employee order by empno,attr;

select empno, 
       trim(substr(k_v from position('name' in k_v)+20 for 20)) as name,
       trim(substr(k_v from position('address' in k_v)+20 for 20)) as address,
       trim(substr(k_v from position('title' in k_v)+20 for 20)) as title
  from (select empno, group_concat(attr||value order by attr separator '') as k_v
          from employee
        group by empno) as foo;

运行结果如下,

SQL>select * from employee order by empno,attr;

EMPNO       ATTR                 VALUE
----------- -------------------- --------------------
          1 address              Address A
          1 name                 Kevin
          1 title                Junior Engineer
          2 address              Address B
          2 name                 Eason
          2 title                Senior Engineer
          3 address              Address C
          3 name                 Hank
          3 title                Architecture

--- 9 row(s) selected.

SQL>select empno,
           trim(substr(k_v from position('name' in k_v)+20 for 20)) as name,
           trim(substr(k_v from position('address' in k_v)+20 for 20)) as address,
           trim(substr(k_v from position('title' in k_v)+20 for 20)) as title
  from (select empno, group_concat(attr||value order by attr separator '') as k_v
                  from employee
                group by empno) as foo;

EMPNO       NAME                 ADDRESS              TITLE
----------- -------------------- -------------------- --------------------
          1 Kevin                Address A            Junior Engineer
          2 Eason                Address B            Senior Engineer
          3 Hank                 Address C            Architecture

--- 3 row(s) selected.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据源的港湾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值