Aggregate bitwise-OR in a subquery

后端 未结 10 2035
醉酒成梦
醉酒成梦 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:15

    For me that is the best solution.

    declare @res int
    set @res=0    
    SELECT  @res=@res|t.n
        FROM    ( SELECT    1 AS n
                  UNION ALL
                  SELECT    2
                  UNION ALL
                  SELECT    3
                  UNION ALL
                  SELECT    4
                  UNION ALL
                  SELECT    5
                  UNION ALL
                  SELECT    6
                ) AS t
    

提交回复
热议问题