1

对于这样结构的表:

"Object Attribute" Table - Links objects with all of their attributes and values;
Object ID       Attribute ID    Attribute String Value  ...
1337            123             Example Object Title
1337            124             Example Object Type
1337            125             Example Object Description
1337            126             Example Object Author
0001            123             (null)
0001            124             SomeType
0001            125             This is an example record
0001            126             Jiman
0002            123             Bar
0002            124             BarType
0002            125             This is another
0002            126             Jiman

编辑:属性 ID 具有以下映射:

Attribute ID    Attribute Name
123             Title
124             Type
125             Description
126             Author

我将如何编写返回一组转置数据的 Oracle 查询,其中 Title 字段为 NULL?

示例输出:

ID      Title       Type        Description             Author
0001    (null)      FooType     This is an example...   Jiman

PIVOT 可以用来转置非聚合值(即那些字符串属性)吗?

4

1 回答 1

2

最简单的方法,假设您希望在查询中硬编码从属性 ID 到属性名称的映射,并且您在编译时知道结果中想要的元素数量,就像

SELECT *
  FROM (
    SELECT object_id,
           MAX( CASE WHEN attribute_id = 123 THEN attribute_string_value ELSE NULL END) title,
           MAX( CASE WHEN attribute_id = 124 THEN attribute_string_value ELSE NULL END) type,
           MAX( CASE WHEN attribute_id = 125 THEN attribute_string_value ELSE NULL END) description,
           MAX( CASE WHEN attribute_id = 126 THEN attribute_string_value ELSE NULL END) author
      FROM your_table_name
     GROUP BY object_id )
  WHERE title IS NULL

如果只需要支持11g,也可以使用PIVOToperator。

于 2012-10-19T17:56:09.337 回答