简体   繁体   English

SQL:基于行值动态重命名列

[英]SQL: Dynamic renaming of columns based on row values

Considering Oracle 10g , is there a way to rename a column based on specified rows? 考虑到Oracle 10g ,有没有办法根据指定的行重命名列?

Let me first give a background. 我先来介绍一下背景。 The requirement was to pivot rows and turn them to columns. 要求是将行转动并将它们转换为列。 Since Oracle 10g doesn't support the function PIVOT, we've done a work around using max and case keywords. 由于Oracle 10g不支持PIVOT功能,我们已经完成了使用max and case关键字的工作。

Now the problem is this. 现在的问题是这个。 Is there a way to rename a column based on two rows? 有没有办法根据两行重命名列? Considering the tables below: 考虑下表:

BRAND   | MODEL  | COMPONENT_NAME | COMPONENT_VALUE | ATTRIBUTE_NAME | ATTRIBUTE_VALUE
-----------------------------------------------------------------------------------
SAMSUNG | I9100  | Chipset        | Exynos          | frequency      | 1200     
SAMSUNG | I9100  | Screen         | Amoled          | colors         | 16M  
SAMSUNG | I9100G | Chipset        | TI OMAP4430     | frequency      | 1200     
SAMSUNG | I9100G | Screen         | Amoled          | colors         | 16M 
------------------------------------------------------------------------------------

We want this: with the COMPONENT_NAME above transformed as the `column header for one and the COMPONENT_NAME - ATTRIBUTE_NAME` as for another. 我们想要这样:将上面的COMPONENT_NAME转换为`column header for one and the COMPONENT_NAME - ATTRIBUTE_NAME`作为另一个。

BRAND   | MODEL  | Chipset     | Chipset - frequency | Screen | Screen - colors
------------------------------------------------------------------------
SAMSUNG | I9100  | Exynos      | 1200                | Amoled | 16M        
SAMSUNG | I9100G | TI OMAP4430 | 1200                | Amoled | 16M
------------------------------------------------------------------------

currently we do the following to produce the second table: 目前我们执行以下操作来生成第二个表:

SELECT DISTINCT BRAND, MODEL,
MAX(CASE WHEN (COMPONENT_NAME = 'Chipset') THEN
COMPONENT_VALUE
END) AS "Chipset",
MAX(CASE WHEN (COMPONENT_NAME = 'Chipset' and ATTRIBUTE_NAME = 'frequency') THEN
ATTRIBUTE_VALUE
END) AS "Screen",
MAX(CASE WHEN (COMPONENT_NAME = 'Screen') THEN
COMPONENT_VALUE
END) AS "Screen",
MAX(CASE WHEN (COMPONENT_NAME = 'Screen' and ATTRIBUTE_NAME = 'colors') THEN
ATTRIBUTE_VALUE
END) AS "Screen - colors" from table....etc.

Is there a way to dynamically name the column? 有没有办法动态命名列?

A column name is analogous to a variable name - it's an identifier that may be used in a program. 列名称类似于变量名称 - 它是可以在程序中使用的标识符。 It doesn't make much sense for its name to change dynamically. 它的名称动态变化没有多大意义。

Your current strategy of having different columns for each attribute is ok, IMO. IMO,您当前为每个属性设置不同列的策略是可以的。

You are experiencing the downside to having an EAV data model. 您正在体验拥有EAV数据模型的不利方面。

You want to execute a dynamic sql statement into a SYS_REFCURSOR 您希望将动态sql语句执行到SYS_REFCURSOR中

However, you're causing considerable overhead, because 但是,你造成了相当大的开销,因为

  1. you'd have to execute the query twice (1st to build the dynamic SQL, 2nd to retrieve the result) 你必须执行两次查询(第一次构建动态SQL,第二次检索结果)
  2. you're hard-parsing more often than would be neccessary 你需要更多的解析而不是必要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM