1

Is it possible to create a binary matrix in AWS Athena from a table. For example we have the following table:

name product
John Bike
John Shirt
John Ball
Blake Shirt
Mike Ball
Mike Hat

To be converted to the following:

name Bike Shirt Ball Hat
John 1 1 1 0
Blake 0 1 0 0
Mike 0 0 1 1

1 Answers1

1

I suggest you to use Case expression:

select name, 
sum(case when product = 'bike' then 1 else 0 end) as "Bike",
sum(case when product = 'Shirt' then 1 else 0 end) as "Shirt",
sum(case when product = 'Ball' then 1 else 0 end) as "Ball",
sum(case when product = 'Hat' then 1 else 0 end) as "Hat"
from tableName