首页 > 解决方案 > 将包含列值的非固定值的行动态转置为 Oracle 中的列名

问题描述

我有一个查询结果(在连接多个具有主子关系和查找关系的表时),它返回具有一些重复列值的多行。示例结果集如下所示。

ReviewID | UserId | ProductId | ReviewDate | UserAgent | ReviewQuestionId | ReviewQuestionText | ReviewResponse | ReviewCommentId | ReviewComment

1  | 40001 | 2005 | 01/01/2010 | Mobile | 45 | Product_Availability | 10 | 10002 | I am happy with the product xxxxx

1  | 40001 | 2005 | 01/01/2010 | Mobile | 46 | Location_Access | 5 | 10002 | I am happy with the product xxxxx

1  | 40001 | 2005 | 01/01/2010 | Mobile | 47 | Staff | 8 | 10002 | I am happy with the product xxxxx

1  | 40001 | 2005 | 01/01/2010 | Mobile | 48 | Shipping | 9 | 10002 | I am happy with the product xxxxx

您可以看到,除了 ReviewQuestionId | 评论问题文本 | ReviewResponse,所有其他列都在重复。我想要 1 行中的响应,其中 ReviewQuestionText 将是列名,ReviewResponse 是该列中的值。

ReviewID | UserId | ProductId | ReviewDate | UserAgent | Product_Availability | Location_Access | Staff| Shipping | ReviewCommentId | ReviewComment
     
1  | 40001 | 2005 | 01/01/2010 | Mobile | 10 | 5 | 8 | 9 | 10002 | I am happy with the product xxxxx

ReviewQuestionId 和 ReviewQuestionText 列未针对评论进行修复。根据进行审查的产品和年份,问题集是不同的,因此我无法修复我的枢轴中的列。

标签: sqloracleoracle11gpivot

解决方案


推荐阅读