Search code examples
sqlgroup-bygoogle-bigqueryaggregate

Aggregate values to FALSE if any of the value contains FALSE GROUP BY a column


I got the following table

FFId PId PDId Emp Operating
885 3022 816 1090A true
871 3022 816 1090A false
839 7465 7196 989A true
839 7465 7196 1094A true
782 3994 4290 1176A true
796 3994 4290 1176A false

And I would like to aggregate the Operating value into false when there are either true or false with the same PId, PDId, Emp

So the expected result would be

FFId PId PDId Emp Operating New Operating
885 3022 816 1090A true false
871 3022 816 1090A false false
839 7465 7196 989A true true
839 7465 7196 1094A true true
782 3994 4290 1176A true false
796 3994 4290 1176A false false

Could you please help me here to generate SQL query. Thanks!


Solution

  • Consider below (BgQuery Standard SQL)

    select *, 
      logical_and(Operating) over(partition by PId, PDId, Emp) as New_Operating
    from your_table    
    

    if applied to sample data in your question - output is

    enter image description here