这是我有一个示例表
CustomerName|Items
customer 1 |Item 1
customer 1 |Item 2
customer 1 |Item 3
customer 2 |Item 1
customer 2 |Item 4
customer 3 |Item 2
customer 4 |Item 5
customer 5 |Item 6
我需要将表格转换为
CustomerName|Item 1|Item 2|Item 3|Item 4|Item 5|Item 6|
customer 1 | X | X | X | | | |
customer 2 | X | | | X | | |
customer 3 | | X | | X | | |
customer 4 | | | | | X | |
customer 5 | | | | | | X |
我尝试了PIVOT
,在SAP HANA中无效。
我们非常感谢任何建议。
答案 0 :(得分:1)
SAP HANA未提供可在其他DBMS中找到的非标准SQL运算符“PIVOT”。 对于永久转换,例如在ETL期间,您可以使用流程图功能PIVOT / UNPIVOT自动执行转换。
对于即时转换,没有内置功能,您必须自己编写代码。
答案 1 :(得分:0)
好的,试试这个:
SELECT CustomerName,
MAX(CASE WHEN ROWN=1 THEN "Items" ELSE NULL END) AS Item1,
MAX(CASE WHEN ROWN=2 THEN "Items" ELSE NULL END) AS Item2,
MAX(CASE WHEN ROWN=3 THEN "Items" ELSE NULL END) AS Item3,
MAX(CASE WHEN ROWN=4 THEN "Items" ELSE NULL END) AS Item4,
MAX(CASE WHEN ROWN=5 THEN "Items" ELSE NULL END) AS Item5,
MAX(CASE WHEN ROWN=6 THEN "Items" ELSE NULL END) AS Item6,
FROM
(SELECT CustomerName, Items,
row_number() over (PARTITION BY CustomerName) AS ROWN
FROM TABLE_NAME ORDER BY CustomerName)
GROUP BY CustomerName;
其中TABLE_NAME是源表的名称。