通过SQL实现列转行
order_sn | user_id | coupon_sn |
A | 1 | 券A1,券A2,券A3 |
B | 2 | 券B1 |
C | 3 | (NULL) |
D | 4 | 券D1 |
SELECT table_a.order_sn,table_a.user_id,table_b.coupon_sn_new
from table_a
LATERAL VIEW --展示空值需使用 LATERAL VIEW OUTER
EXPLODE(split(coupon_sn,',')) table_b as coupon_sn_new --字符串需转换为数组
;
order_sn | user_id | coupon_sn_new |
A | 1 | 券A1 |
A | 1 | 券A2 |
A | 1 | 券A3 |
B | 2 | 券B1 |
D | 4 | 券D1 |
D | 4 | 券D2 |