How to pivot without aggregation in presto?

by wilmer.lemke , in category: Third Party Scripts , 13 days ago

How to pivot without aggregation in presto?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by dalton_moen , 13 days ago

@wilmer.lemke 

In Presto, you can pivot data without using aggregation functions by using the CASE statement to create separate columns for each distinct value that you want to pivot on.


Here's an example of how you can pivot data without aggregation in Presto:

1
2
3
4
5
6
7
8
9
SELECT
  ID,
  MAX(CASE WHEN category = 'A' THEN value END) AS A,
  MAX(CASE WHEN category = 'B' THEN value END) AS B,
  MAX(CASE WHEN category = 'C' THEN value END) AS C
FROM
  table_name
GROUP BY
  ID;


In this example, we are pivoting the data based on the 'category' column, creating separate columns for each distinct category ('A', 'B', 'C') and their corresponding values using the CASE statement. The MAX function is used to get the maximum value for each category, but since there is only one value for each category, it essentially just performs the pivot without aggregation.