Skip to content

Commit b54128c

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

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

guides/行转列使用.sql

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
原始数据:
3+
id,name,login_date
4+
1,bebee,2019-12-01
5+
2,bebee,2019-12-02
6+
3,jack,2019-12-03
7+
4,bebee,2019-12-04
8+
5,bebee,2019-12-05
9+
6,jack,2019-12-06
10+
7,jack,2019-12-07
11+
8,bebee,019-12-08
12+
13+
目标数据:
14+
name login_date
15+
bebee 2019-12-01,2019-12-02,2019-12-04,2019-12-05,2019-12-08
16+
jack 2019-12-03,2019-12-06,2019-12-07
17+
*/
18+
19+
-- 1. 建表
20+
set hive.exec.mode.local.auto=true; --开启Hive的本地模式
21+
22+
drop table if exists user_login;
23+
create temporary table user_login(
24+
id int,
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 name,concat_ws(',',collect_set(login_date)) as login_date
38+
from user_login
39+
group by name;
40+
41+
42+
43+
44+
45+

0 commit comments

Comments
 (0)