Question

I have some data that looks something like this:

key B
1   true
1   false
2   false
2   false

And I want to roll it up so that if column B has two false values then the resulting value for B is false; otherwise, if there is a true and false value the value for B is true. Like this:

key B
1   true
2   false

Is there a way that this can be done with (Snowflake) SQL? I first thought of a pivot table but this doesn't quite seem to be what pivots do.

Was it helpful?

Solution

According to your text if any of them are true is must be true, otherwise is false:

select
    key,
    case 
      when sum(case when B is true then 1 else 0 end) > 0 then true
      else false
    end as result
from
   t
group by
    key;

db<>fiddle here

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top