I have a input redshift dataset like the one below:
car_id | driver_id | salesman | tyre_id | price_min | price_max | price_avg
-------+-----------+----------+---------+-----------+-----------+----------
A | 1 | 1 | 9 | 1 | 1 | 1
A | 2 | 1 | 9 | 1 | 1 | 1
A | 3 | 1 | 9 | 1 | 1 | 1
A | 4 | 1 | 9 | 1 | 1 | 1
A | 1 | 2 | 7 | 0 | 1 | 1
A | 2 | 2 | 7 | 0 | 0 | 1
A | 3 | 2 | 7 | 0 | 1 | 1
A | 4 | 2 | 7 | 0 | 0 | 0
I would like to pivot multiple columns whose values may be dynamic in nature. I want to pivot the columns salesman, tyre_id with the values price_min, price_avg, price_max. Basically, each driver_id will have 1 single row.
I can achieve it in python but I need a query as my requirement.
I tried concatenating salesman and tyreId. It partially works but I need help from the expert community for this to achieve fully. Can anyone help me and guide me through this?
This is the query I tried:
CREATE TABLE public.temp_car_id
(
car_id VARCHAR(10),
driver_id INT,
salesman INT,
tyre_id INT,
price_min INT,
price_max INT,
price_avg INT
);
INSERT INTO public.temp_car_id
VALUES
('A',1,1,9,0.61,0.89,0.91),
('A',2,1,9,0.63,0.93,0.58),
('A',3,1,9,0.91,0.83,0.99),
('A',4,1,9,0.53,0.84,0.79),
('A',1,2,7,0.12,0.97,0.87),
('A',2,2,7,0.13,0.30,0.84),
('A',3,2,7,0.17,0.62,0.63),
('A',4,2,7,0.09,0.01,0.19);
SELECT car_id,
driver_id,
CONCAT('.',CONCAT(CONCAT(salesman,'.'), tyre_id)) AS sales_tyre,
price_min,
price_max,
price_avg INTO #temp_car_id_unpivot
FROM public.temp_car_id;
SELECT *
FROM #temp_car_id_unpivot PIVOT (MAX(price_min)
FOR
sales_tyre IN ('.1.9','.2.7'));
