1.數(shù)據(jù)集
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
+ ---+----------+ |id |login_date| + ---+----------+ |01 |2021-02-28| |01 |2021-03-01| |01 |2021-03-02| |01 |2021-03-04| |01 |2021-03-05| |01 |2021-03-06| |01 |2021-03-08| |02 |2021-03-01| |02 |2021-03-02| |02 |2021-03-03| |02 |2021-03-06| |03 |2021-03-06| + ---+----------+ |
以"連續(xù)登錄"中的數(shù)據(jù)為例:
1
2
3
4
|
select id, concat_ws( ',' ,collect_list(login_date)) cw from data group by id; |
結(jié)果:
+---+----------------------------------------------------------------------------+
|id |cw |
+---+----------------------------------------------------------------------------+
|01 |2018-02-28,2018-03-01,2018-03-02,2018-03-04,2018-03-05,2018-03-06,2018-03-08|
|02 |2018-03-01,2018-03-02,2018-03-03,2018-03-06 |
|03 |2018-03-06 |
+---+----------------------------------------------------------------------------+
以上面SQL生成的數(shù)據(jù)為基準(zhǔn),執(zhí)行下列SQL:
1
2
3
|
select id, login_date from t lateral view explode(split(cw, ',' )) b AS login_date; |
結(jié)果:
+---+----------+
|id |login_date|
+---+----------+
|01 |2018-02-28|
|01 |2018-03-01|
|01 |2018-03-02|
|01 |2018-03-04|
|01 |2018-03-05|
|01 |2018-03-06|
|01 |2018-03-08|
|02 |2018-03-01|
|02 |2018-03-02|
|02 |2018-03-03|
|02 |2018-03-06|
|03 |2018-03-06|
+---+----------+
到此這篇關(guān)于SQL行轉(zhuǎn)列與列轉(zhuǎn)行的文章就介紹到這了,更多相關(guān)SQL行轉(zhuǎn)列與列轉(zhuǎn)行內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/weixin_38037405/article/details/122083226