Skip to content
Advertisement

Case when value of a group is 1 then all values of that group should be replaced by 1 – SQL

I’m writing a query (using Athena – AWS) and I need to substitute all values from a group if there’s at least one occurrence of another value. To exemplify:

My original dataframe

What I need: case when v1, v2 or v3 assume value 1 group by ID, then the whole column that contains 1 group by ID should be 1.

This is the output I need:

Since v1, v2, v3 had at least one number 1 in the group ID 123, then all these columns for this group should be 1. However, ID 456 had at least one number 1 only in column v1, then v1 will be 1 and v2, v3 will remain 0.

Does someone know how to do that?

Advertisement

Answer

You may try the following

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement