Transpose from rows into columns by using sql or pivot database function.
Data preparation:
CREATE VOLATILE TABLE product
(
id INTEGER,
name VARCHAR(10),
dimension VARCHAR(10),
dimension_value INTEGER
)
ON COMMIT PRESERVE ROWS;
INSERT INTO product(1,'fridge','depth',55);
INSERT INTO product(1,'fridge','height',120);
INSERT INTO product(1,'fridge','width',60);
INSERT INTO product(2,'heater','depth',30);
INSERT INTO product(2,'heater','height',35);
INSERT INTO product(2,'heater','width',25);
SELECT * from product order by 1,3;
/*
id name dimension dimension_value
1 fridge depth 55
1 fridge height 120
1 fridge width 60
2 heater depth 30
2 heater height 35
2 heater width 25
*/
Solution using SQL
SELECT * FROM
(
SELECT
id ,
name ,
SUM(CASE WHEN dimension = 'depth' THEN dimension_value ELSE NULL END) AS depth ,
SUM(CASE WHEN dimension = 'height' THEN dimension_value ELSE NULL END) AS height ,
SUM(CASE WHEN dimension = 'width' THEN dimension_value ELSE NULL END) AS width
FROM product
GROUP BY 1,2
) X
ORDER BY 1
;
/*
id name depth height width
1 fridge 55 120 60
2 heater 30 35 25
*/
Solution using Teradata PIVOT function
SELECT *
FROM product
PIVOT
(
SUM(dimension_value) FOR dimension
IN
(
'depth' as depth,
'height' as height,
'width' as width
)
)X
ORDER BY 1
;
/*
id name depth height width
1 fridge 55 120 60
2 heater 30 35 25
*/


Leave a Reply