Skip to content

Commit c4f0c5b

Browse files
committed
📝列转行介绍
1 parent b54128c commit c4f0c5b

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

guides/列转行使用.sql

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
原始数据:
3+
name,login_date
4+
bebee,2019-12-01|2019-12-02|2019-12-04|2019-12-05|2019-12-08
5+
jack,2019-12-03|2019-12-06|2019-12-07
6+
7+
目标数据:
8+
name login_date
9+
bebee 2019-12-01
10+
bebee 2019-12-02
11+
jack 2019-12-03
12+
bebee 2019-12-04
13+
bebee 2019-12-05
14+
jack 2019-12-06
15+
jack 2019-12-07
16+
bebee 2019-12-08
17+
18+
*/
19+
20+
-- 1. 建表
21+
set hive.exec.mode.local.auto=true; --开启Hive的本地模式
22+
23+
drop table if exists user_login;
24+
create temporary table user_login(
25+
name string,
26+
login_date string
27+
) row format delimited fields terminated by ','
28+
stored as textfile
29+
tblproperties(
30+
"skip.header.line.count"="1" --跳过文件首的1行
31+
--"skip.footer.line.count"="n" --跳过文件尾的n行
32+
);
33+
34+
load data local inpath '/tmp/sgr/user_login.csv' into table user_login;
35+
36+
-- 2. 处理
37+
select a.name,b.login_date
38+
from user_login a
39+
lateral view explode(split(login_date,'\\|')) b AS login_date
40+
order by b.login_date;
41+
42+
43+
44+
45+
46+

0 commit comments

Comments
 (0)