SQL行转列与列转行

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

关于SQL行转列与列转行的文章就介绍至此,更多相关SQL行转列与列转行内容请搜索编程宝库以前的文章,希望以后支持编程宝库

今天小编给大家推荐一个超强交互式可视化绘制工具-python-highcharts,熟悉HightCharts绘图软件的小伙伴对这个不会陌生,python-highchart ...