当前位置:主页 > 数据库 > Mysql >

SQL行转列与列转行详情

时间:2022-06-23 11:39:12 | 栏目:Mysql | 点击:

1.数据集

+---+----------+
|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|
+---+----------+

以"连续登录"中的数据为例:

select id, 
       concat_ws(',',collect_list(login_date)) cw
from data
group by id;

结果:

+---+----------------------------------------------------------------------------+
|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生成的数据为基准,执行下列SQL:

select id, login_date
from t 
lateral  view explode(split(cw,','))  b AS login_date;

结果:

+---+----------+
|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|
+---+----------+

您可能感兴趣的文章:

相关文章