## Case Study: Storing, Querying, and Analyzing Performance: Pt. 2December 15, 2000 ## Querying Responses## Problem:Having stored our checkbox data as integers how does one query the table to find the corresponding bits which are set? ## Background:Although the pervasiveness of TCP/IP and network masks has made more people aware of the binary number system let's review how to figure out which bits are set. Given an integer how does one calculate which particular bits are set. A math teacher would give a better explanation on the terminology behind the following process but the following is a process one can use. ## The Process:The following is a list of bits. As I mentioned in the previous article each bit corresponds to a given checkbox.
In the last article Storing Responses the process of storing checkbox data as integers was demonstrated. Following the procedures from that article on may end up with an integer value of 37 stored in the database. Given 37 is a value in the database how would one go backwards to figure out which bits this integer corresponds. The manual process uses the following logic. Find the bit corresponding to a value not larger than 37 but as close to 37 as possible. In this case the value would be 32 which means bit 6 is set. Subtract 37 from the value of the set bit. This will leave 5. Now do the process again. The value closest to 5 without going over is 4, this means bit 3 is set. Take away the 4 from 5 and you are left with 1 this means bit 1 is set. So to represent 37 in binary format we need bits 6, 3 and 1 set. Programming the above logic can be done. It is a long and tedious process. Another method would be to use the following formula for each of the bits we need to test (Note: we will only need to test the bit greater than the largest integer in the table). Below is the example for each bit followed by the corresponding SQL statement. ## Expressions to Return Each Bit
Because both Access and SQL can be used to query a SQL database and both are used in my current working environment I have included the SQL statements in using the syntax for each application. ## SQL Statement (Access):
To specify a specific bit add a
## SQL Statement (Transact-SQL):
To specify a specific bit add a
It is now possible to write a query to return counts and other information from the survey data mentioned in the first article. The next article will discuss performance and other miscellaneous aspects of this table design method. |