1、演示多列轉為單行
資料檔案及內容: student.txt
xiaoming|english|92.0
xiaoming|chinese|98.0
xiaoming|math|89.5
huahua|chinese|80.0
huahua|math|89.5
建立表studnet:
create table student(name string,subject string,score decimal(4,1))
row format delimited
fields terminated by '|';
匯入資料:
load data local inpath '/home/hadoop/hivetestdata/student.txt' into table student;
列轉為行演示:
hive (hive)>select name,concat_ws(',',collect_set(subject)) from student group by name;
huahua
chinese,math
xiaoming
english,chinese,math
hive (hive)>select name,concat_ws(',',collect_set(concat(subject,'=',score))) from student group by name;
huahua
chinese=80,math=89.5
xiaoming
english=92,chinese=98,math=89.5
2、演示單行轉為多列
資料檔案及內容:student2.txt
huahua|chinese=80,math=89.5
xiaoming|english=92,chinese=98,math=89.5
建立表:
create table student2(name string,subject_score_list string)
row format delimited
fields terminated by '|';
匯入資料:
load data local inpath '/home/hadoop/hivetestdata/student2.txt' into table student2;
行轉為列演示:
hive (hive)> select * from student2;
student2.name
student2.subject_score_list
huahua
chinese=80,math=89.5
xiaoming
english=92,chinese=98,math=89.5
hive (hive)> select name, subject_list from student2 stu2lateral view explode(split(stu2.subject_score_list,','))stu_subj as subject_list;
----別名一定不要忘記
huahua
chinese=80
huahua
math=89.5
xiaoming
english=92
xiaoming
chinese=98
xiaoming
math=89.5
impala的行列轉換請檢視:
Oracle中行列轉換方法
實際查詢中,可能會出現將某些查詢結果值顯示在列位置上,此時可之直接通過sql語句進行處理 1.方法 使用sql的函式轉換 select aa.po no,aa.po line,nvl case when aa.group name re then aa.qty end 0 as re,nvl cas...
Hive 行列轉換
在京東眾多業務中,業務充滿了複雜性和挑戰性,因為業務的靈活性,很多資料都儲存成xml和json格式資料,這就要求下游資料分析師們需要對其做解析後方可使用 在眾多操作中 有一種是需要對資料做行列轉換操作。資料結構 create external table jd row to column jd id...
mysql中行列表示 mysql 行列轉換怎麼寫?
一段sqlserversql 完成行列轉換,但在mysql裡無法執行,請問mysql的應該怎麼寫?createtablea1 no1int,no2varchar 10 mya1int,mya2int,mya3int,mya4int,mya5int,mya6int inserta1.一段sqlserv...