我有一个具有以下记录的UPC表
SKU ATTR_NAME ATTR_VALUE
--------- --------- ----------
38890630 COLOR Black
38890630 DISC Y
38890630 SIZE 8
我希望输出如下
SKU COLOR SIZE
--------- ------ ----
38890630 Black 8
我尝试了多种方法,但无法获得所需的输出。有人可以帮忙吗?
答案 0 :(得分:1)
您可以使用条件聚合:
select sku,
max(case when attr_name = 'COLOR' then attr_value end) as color,
max(case when attr_name = 'DISC' then attr_value end) as disc,
max(case when attr_name = 'SIZE' then attr_value end) as size
from t
group by sku;
答案 1 :(得分:0)
您可以使用PIVOT查询:
WITH UPC_TABLE AS (
SELECT 38890630 AS SKU, 'COLOR' AS ATTR_NAME, 'BLACK' AS ATTR_VALUE FROM DUAL
UNION ALL SELECT 38890630 AS SKU, 'DISC' AS ATTR_NAME, 'Y' AS ATTR_VALUE FROM DUAL
UNION ALL SELECT 38890630 AS SKU, 'SIZE' AS ATTR_NAME, '8' AS ATTR_VALUE FROM DUAL
)
SELECT * FROM
UPC_TABLE PIVOT (MAX(ATTR_VALUE) AS ATTR_VALUE FOR (ATTR_NAME) IN (
'COLOR' AS COLOR
, 'DISC' AS DISC
, 'SIZE' AS SIZE_FIELD
)
);