1. 程式人生 > >hive sql 行列轉換

hive sql 行列轉換

-- 對一張大表的每一行,後面加多種label值
-- 其實就是笛卡爾積,舉例
-- SELECT * FROM dev.dev_jiadian_user_yuge_temp 
-- CROSS JOIN
-- (
--     SELECT 0 AS label
--     UNION ALL
--     SELECT 1 AS label
--     UNION ALL
--     SELECT 2 AS label
-- )t;

-- 方法二
-- 使用陣列,然後進行轉置操作
-- 注意k是flag別名,必不可少

SELECT *
FROM
(
SELECT *,
       Array(0,1,2) AS flag
FROM
    dev.dev_jiadian_user_yuge_temp
 )t
lateral view  explode(flag)k AS label
;

-- 

  

參考:https://blog.csdn.net/wangguohe/article/details/79386917

         https://blog.csdn.net/dreamingfish2011/article/details/51250641