将行转换为列sql

时间:2018-04-13 14:48:29

标签: sql oracle

我有一张如下表格。我需要将行转换为列。 我知道我可以通过工会选择5次。我正在寻找有关如何使用pivot

执行此操作的建议
ID    NAME   TAG    TAG_1   TAG_2   TAG_3   TAG_4  LOCATION
10    test   abc    abc_1   abc_2   abc_3   abc_4  china

ID    NAME    TAG        LOCATION
10    test    abc        china
10    test    abc_1      china
10    test    abc_2      china
10    test    abc_3      china
10    test    abc_4      china

with t as (
 select ID, name, tag,location from table_one
 )
 select * from t
 unpivot (
 value for tag in (tag_1,tag_2,tag_3,tag_4
 )

我尝试使用上面的unpivot,我收到列名称(tag_1,tag_2)的无效标识符ORA-00904错误。声明中有什么问题。

4 个答案:

答案 0 :(得分:0)

在SQL SERVER 2008中:

'date'

答案 1 :(得分:0)

我希望我能正确地回答你的问题。你可以在oracle中使用PIVOT来做到这一点。试试这个。如果需要,您可以在PIVOT子句中向列表中添加更多内容。

SELECT *
FROM (SELECT 10 AS id, 'test' AS name, 'abc_1' AS tag, 'china' AS location FROM DUAL
      UNION
      SELECT 10 AS id, 'test' AS name, 'abc_2' AS tag, 'china' AS location FROM DUAL
      UNION
      SELECT 10 AS id, 'test' AS name, 'abc_3' AS tag, 'china' AS location FROM DUAL
      UNION
      SELECT 10 AS id, 'test' AS name, 'abc_4' AS tag, 'china' AS location FROM DUAL
      UNION
      SELECT 10 AS id, 'test' AS name, 'abc_5' AS tag, 'china' AS location FROM DUAL
      UNION
      SELECT 20 AS id, 'test' AS name, 'abc_2' AS tag, 'china' AS location FROM DUAL
      UNION
      SELECT 20 AS id, 'test' AS name, 'abc_4' AS tag, 'china' AS location FROM DUAL
      UNION
      SELECT 20 AS id, 'test' AS name, 'abc_6' AS tag, 'china' AS location FROM DUAL
      UNION
      SELECT 30 AS id, 'test' AS name, 'abc_1' AS tag, 'china' AS location FROM DUAL
      UNION
      SELECT 30 AS id, 'test' AS name, 'abc_3' AS tag, 'china' AS location FROM DUAL
      UNION
      SELECT 30 AS id, 'test' AS name, 'abc_5' AS tag, 'china' AS location FROM DUAL)
     PIVOT (MAX (tag) AS tag FOR (tag) IN ('abc_1' AS t1, 'abc_2' AS t2, 'abc_3' AS t3, 'abc_4' AS t4, 'abc_5' AS t5, 'abc_6' AS t6))

ORDER BY id,name,location;

答案 2 :(得分:0)

create table test_pivot
(TID  number,
Tname  varchar2(10),
Tag_1  varchar2(10),
Tag_2  varchar2(10),
Tag_3   varchar2(10),
Tag_4    varchar2(10),
Tag_5    varchar2(10),
T_Location  varchar2(10)
);


insert into test_pivot(TID,
Tname,
Tag_1,
Tag_2,
Tag_3,
Tag_4,
Tag_5,
T_Location
)
values(10,'Test','ABC','PQR','TUV','LMN','XYZ','INDIA');


select * from test_pivot;

select *
from test_pivot
unpivot(value for tag in (Tag_1 as 'Tag_1',
Tag_2 as 'Tag_2',
Tag_3 as 'Tag_3',
Tag_4 as 'Tag_4',
Tag_5 as 'Tag_5')
)

答案 3 :(得分:0)

    with t as (
     select ID, name, tag, tag_1, tag_2, tag_3, tag_4, location from table_one
     )
     select * from t
     unpivot (
     value for _tag_ in (tag,tag_1,tag_2,tag_3,tag_4)
     )

编辑: 确定了OP的sql中的三个问题:

  1. WITH子句不包含列TAG_1 .. TAG_4,但它们已包含在UNPIVOT子句中。
  2. with t as (
     select ID, name, tag,location from table_one
     )
    
    1. UNPIVOT子句正在使用现有列TAG来收集TAG_1到TAG_4的值。
    2.  unpivot (
       value for tag in (tag_1,tag_2,tag_3,tag_4
       )
      
      1. 预期结果集显示TAG中的值,但未在IN块中列出。
      2.   

        (tag_1,tag_2,tag_3,tag_4

        中标记的值)