Bit Functions
The bit aggregate functions work in the same way that bit operators work.When values in a grouped column are calculated using a bit function,
the bits are compared and a value is returned. The way in which the bits are compared depends on the
bit function being used. The first bit aggregate function is the BIT_AND() function.
The BIT_AND() function is similar to the bitwise AND (&) comparison operator in the way in which the function compares bits. For any two or more values, the bits in each bit position are compared. Based on the comparison, a 1 is returned if all bits in a bit position equal 1; otherwise, a 0 is returned. For example,
suppose that the Classes table that you have been using in this section includes an additional column
that tracks attributes that describe the class, such as room requirements or special equipment. You are
developing an application that uses the Attributes column to determine class-specific needs. The following
SELECT statement uses the BIT_AND() function to compare bits in the Attributes column:
SELECT Dept, Level, BIT_AND(Attributes) AS BitwiseAND
FROM Classes
GROUP BY Dept, Level WITH ROLLUP;
This function is similar to the bitwise OR (|) bit operator. The function compares bits in each bit position and returns 1 if at least one bit is 1; otherwise, 0 is returned. You can use the BIT_OR() function as you use the BIT_AND() function:
SELECT Dept, Level, BIT_OR(TotalStudents) AS BitwiseOR
FROM Classes
GROUP BY Dept, Level WITH ROLLUP;
This function is similar to the bitwise exclusive XOR (^) bit operator in that it compares bits and returns 1 only if exactly one of the bit equals 1; otherwise, 0 is returned
SELECT Dept, Level, BIT_XOR(TotalStudents) AS BitwiseXOR
FROM Classes
GROUP BY Dept, Level WITH ROLLUP;
No comments:
Post a Comment