Merge multiple rows in table setting one column based on value in rows
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.
La 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
Licencié sous: CC-BY-SA avec attribution
Non affilié à dba.stackexchange