如何在 HiveQL 中实现 PIVOT 功能。以下是 SQL 查询,请帮助我在 Hive 中实现这一点:
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
-- 期望输出: 定制产品数量 KATE VEG 2 KATE SODA 6 KATE MILK 1 KATE BEER 12 FRED MILK 3 FRED BEER 24 KATE VEG 3