Considering Oracle 10g
, is there a way to rename a column based on specified rows?
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.
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.
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?