赞
踩
对于行专列和列转行,有两种不同的理解,这里将两中不同的理解都整理出来,供大家参考。
name | subject | score |
---|---|---|
Tom | Math | 85 |
Tom | Chinese | 92 |
Tom | English | 88 |
Jack | Math | 92 |
Jack | Chinese | 86 |
Jack | English | 70 |
Korry | Math | 100 |
Korry | Chinese | 100 |
Korry | English | 90 |
select name,
sum(case subject when 'Math' then score else 0 end) as Math,
sum(case subject when 'Chinese' then score else 0 end) as Chinese,
sum(case subject when 'English' then score else 0 end) as English
from tab_scores
group by name
name | Math | English | Chinese |
---|---|---|---|
Tom | 85 | 92 | 88 |
Jack | 92 | 86 | 70 |
Korry | 100 | 100 | 90 |
name | Math | English | Chinese |
---|---|---|---|
Tom | 85 | 92 | 88 |
Jack | 92 | 86 | 70 |
Korry | 100 | 100 | 90 |
select name,'Math',Math from tab_scores
union all
select name,'English',English from tab_scores
union all
select name,'Chinese',Chinese from tab_scores
name | subject | score |
---|---|---|
Tom | Math | 85 |
Tom | Chinese | 92 |
Tom | English | 88 |
Jack | Math | 92 |
Jack | Chinese | 86 |
Jack | English | 70 |
Korry | Math | 100 |
Korry | Chinese | 100 |
Korry | English | 90 |
name | hobby |
---|---|
rose | eat |
rose | sleep |
korry | game |
korry | basketball |
korry | run |
select name,concat_ws(',',collect_set(hobby)) as hobby
from hobbyInfo group by name
name | hobby |
---|---|
rose | eat,sleep |
korry | game,baseketball,run |
name | hobby |
---|---|
rose | eat,sleep |
korry | game,baseketball,run |
select a.name,tabHobby.colHobby as hobby from hobbyInfo a
lateral view explode(split(a.hobby,',')) tabHobby as colHobby
-- tabHobby是侧视图的别名,colHobby是解析出来的字段名,#注意,as不能省略
name | hobby |
---|---|
rose | eat |
rose | sleep |
korry | game |
korry | basketball |
korry | run |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。