CREATE TABLE customer_data
(
user_id INT,
session_id STRING,
source STRING,
purchase INT,
metric INT,
amount STRING
);
INSERT INTO customer_data VALUES
(101, 'xyz789', 'bing', 2, 10, '1500'),
(101, 'xyz789', 'bing', 2, 20, '30000'),
(101, 'xyz789', 'bing', 2, 30, '2K to 4K'),
(202, 'uvw456', 'twitter', 1, 10, '3200'),
(202, 'uvw456', 'twitter', 1, 20, '65000'),
(202, 'uvw456', 'twitter', 1, 30, '4K to 6K');
SELECT * FROM customer_data ORDER BY user_id, metric
I’m having trouble modifying this dataset. I need to group by user_id, session_id, source, and purchase while using metric and amount as pivoted columns. The final output should display separate columns for each metric value (10, 20, 30) with their associated amount. I’ve tried using pivot functions but I’m not sure how to set up the query. Any suggestions for how to accomplish this?