阅读498 返回首页    go 阿里云 go 技术社区[云栖]


MaxCompute SQL-列转行和行转列

1. 假设我们在MaxCompute中有两张表,其中一张表是存用户基本信息,另一张表是存用户的地址信息等,表数据假设如下:

user_basic_info:

id

name

1

a

2

b

3

c

4

d

 

user_address;

name

address

a

add1

a

add2

b

add3

c

add4

d

add5

 

我们可以看到同一个用户不止一个地址(这里是假设的),我们需要把数据变为如下格式:

id

name

address

1

a

add1,add2

2

b

add3

3

c

add4

4

d

add5

 

 

建表:

CREATE TABLE user_basic_info   (

 id string,

 name string

);

 

CREATE TABLE user_address   (

 name string,

 address string

);

插入数据:

insert into table user_basic_info

select '1','a' from (select count(1) from user_basic_info) t;

 

insert into table user_address

select 'a','add1' from (select count(1) from user_address) t;

 

执行合并:

select ubi.id, ubi.name, wm_concat(',', ua.address) as address from user_basic_info ubi join user_address ua on ubi.name=ua.name GROUP BY ubi.id, ubi.name;

 

运行结果:

运行结果:

1       a       add1,add2

2       b       add3

3       c       add4

4       d       add5

 

 

2. 假设我们有一张表:

user_info:

id

name

address

1

a

add1,add2

2

b

add3

3

c

add4

4

d

add5

 

我们需要拆分address,变为:

id

name

address

1

a

add1

1

a

add2

2

b

add3

3

c

add4

4

d

add5

 

建表

CREATE TABLE user_info   (

 id string,

 name string,

 address string

);

插入数据(导入第一个实验的结果)

insert into table user_info select ubi.id, ubi.name, wm_concat(',', ua.address) as address from user_basic_info ubi join user_address ua on ubi.name=ua.name GROUP BY ubi.id, ubi.name;

 

执行拆分:

select id, name, add_new from user_info ui lateral view explode(split(ui.address,',')) adtable as add_new;

 

 

结果为:

1       a       add1
1       a       add2
2       b       add3
3       c       add4
4       d       add5

 

最后更新:2017-06-27 17:31:56

  上一篇:go  开源智能家居工具整理
  下一篇:go  软件质量没有银弹:阿里巴巴的25个技术实践与坑