Merge multiple rows in table setting one column based on value in rows
Frage
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.
Lösung
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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit dba.stackexchange