Aggregate bitwise-OR in a subquery

后端 未结 10 2023
醉酒成梦
醉酒成梦 2020-11-30 08:46

Given the following table:

CREATE TABLE BitValues ( n int )

Is it possible to compute the bitwise-OR of n for all rows with

10条回答
  •  长情又很酷
    2020-11-30 09:19

    Preparations:

    if object_id(N'tempdb..#t', N'U') is not null drop table #t;
    create table #t ( n int );
    insert into #t values (1), (2), (4), (3);
    

    Solution:

    select max(n & 8) + max(n & 4) + max(n & 2) + max(n & 1) from #t;
    

提交回复
热议问题