如何在HiveQL中實現PIVOT功能。以下是SQL查詢,請您幫我在蜂巢實現這一目標:如何在Hive中PIVOT數據
CREATE TABLE Product(Cust VARCHAR(25), Product VARCHAR(20), QTY INT)
GO
-- Inserting Data into Table
INSERT INTO Product(Cust, Product, QTY) VALUES('KATE','VEG',2)
INSERT INTO Product(Cust, Product, QTY) VALUES('KATE','SODA',6)
INSERT INTO Product(Cust, Product, QTY) VALUES('KATE','MILK',1)
INSERT INTO Product(Cust, Product, QTY) VALUES('KATE','BEER',12)
INSERT INTO Product(Cust, Product, QTY) VALUES('FRED','MILK',3)
INSERT INTO Product(Cust, Product, QTY) VALUES('FRED','BEER',24)
INSERT INTO Product(Cust, Product, QTY) VALUES('KATE','VEG',3)
GO
-- Pivot Table ordered by PRODUCT
SELECT PRODUCT, FRED, KATE FROM (
SELECT CUST, PRODUCT, QTY
FROM Product) up
PIVOT (SUM(QTY) FOR CUST IN (FRED, KATE)) AS pvt
ORDER BY PRODUCT
--Desired輸出: 卡斯特產品數量 KATE VEG 2 KATE SODA 6 KATE牛奶1 KATE啤酒12 FRED MILK 3 FRED BEER 24 KATE VEG 3
感謝您的回覆。但是,我所看到的是針對PIVOT的Hive查詢。在SQL中的以下查詢和我看起來相當於HiveQL。 - 透視表按產品 SELECT訂購產品,FRED,凱特( SELECT CUST,產品,數量 從產品)達 PIVOT(SUM(數量)FOR CUST IN(FRED,KATE))AS PVT ORDER BY產品 – 2013-03-28 03:59:36
答案與問題無關。 – Tagar 2015-08-10 21:45:52