Oracle 11g Pivot不同的行

时间:2012-05-23 21:11:43

标签: sql oracle oracle11g distinct pivot

我有这样的数据:

**ATTR_ID**    **ATTR_VAL**
103            E
104            F             
136            E
136            U

我想使用PIVOT将数据转换为这个:

**103**     **104**     **136**
E           F           U
E           F           E

在PIVOT中使用MAX()会产生一条记录。我需要得到两个记录。属性id可以有多个属性值。

SELECT *
( SELECT ATTR_ID, ATTR_VAL FROM DATA_TABLE )
PIVOT (MAX(ATTR_VAL) FOR ATTR_ID IN (103,104,136))

我是否使用PIVOT解决了这个错误?

1 个答案:

答案 0 :(得分:0)

如果您只想要笛卡尔积?

SELECT attr_103.attr_val as "103",
       attr_104.attr_val as "104",
       attr_136.attr_val as "136"
  FROM (SELECT attr_id, attr_val
          FROM attr_table
         WHERE attr_id = 103) attr_103
       CROSS JOIN 
       (SELECT attr_id, attr_val
          FROM attr_table
         WHERE attr_id = 104) attr_104
       CROSS JOIN 
       (SELECT attr_id, attr_val
          FROM attr_table
         WHERE attr_id = 136) attr_136