Cross-tabulating ranges of values

You can build a crosstab where each row tabulates a range of values, instead of one discrete value, and you can make each column in the crosstab correspond to a range of values.

For example, in cross-tabulating departmental salary information, you might want one row in the crosstab to count all employees making between $30,000 and $40,000, the next row to count all employees making between $40,000 and $50,000, and so on.

StepsTo cross-tabulate ranges of values:

  1. Determine the expression that results in the raw values being converted into one of a small set of fixed values.

    Each of those values will form a row or column in the crosstab.

  2. Specify the expression in the Columns or Rows box in the Crosstab Definition dialog box.

    You choose the box depending on whether you want the columns or rows to correspond to the range of values.

  3. In the Values column, apply the appropriate aggregate function to the expression.

Example

This is best illustrated with an example.

You want to know how many employees in each department earn between $30,000 and $40,000, how many earn between $40,000 and $50,000, how many earn between $50,000 and $60,000, and so on. To do this, you want a crosstab where each row corresponds to a $10,000 range of salary.

The first step is to determine the expression that, given a salary, returns the next smaller salary that is a multiple of $10,000. For example, given a salary of $34,000, the expression would return $30,000, and given a salary of $47,000, the expression would return $40,000. You can use the Int function to accomplish this, as follows:

int(salary/10000) * 10000

That expression divides the salary by 10,000 and takes the integer portion, then multiplies the result by 10,000. So for $34,000, the expression returns $30,000, as follows:

34000/10000 = 3.4
int(3.4) = 3
3 * 10000 = 30000

With this information you can build the crosstab. The following uses the Employee table in the EAS Demo DB:

  1. Build a crosstab and retrieve the dept_id and salary columns.

  2. In the Crosstab Definition dialog box, drag the dept_id column to the Columns box.

  3. Drag the salary column to the Rows box and to the Values box and edit the expressions.

    In the Rows box, use:

    int(salary/10000) * 10000
    

    In the Values box, use:

    count(int(salary/10000) * 10000 for crosstab)
    

    For more on providing expressions in a crosstab, see “Using expressions”.

  4. Click OK.

    This is the result in the Design view:

    In the leftmost area of the Header 1 band, you will see the title and subtitle. The next area is blank. The third area reads "Total number of employees making the salary." Next is the Header 2 band. In the leftmost area is the truncated expression display for Salary. Department I D is at the top of the third. In the Detail band, row _ column, val, and cross tab sum ( one ) are in each of the three areas, respectively. Next is the Summary band. The leftmost area displays "Total number of employees in the department" and the second area shows the truncated version of the expression used to calculate the totals.

    This is the crosstab at runtime:

    Shown is the output of the cross tab described in the previous illustration.

You can see, for example, that 2 people in department 400 and 5 in department 500 earn between $20,000 and $30,000.

Displaying blank values as zero

In the preceding crosstab, several of the cells in the grid are blank. There are no employees in some salary ranges, so the value of those cells is null. To make the crosstab easier to read, you can add a display format to fields that can have null values so that they display a zero.

StepsTo display blank values in a crosstab as zero:

  1. Select the column you want to modify and click the Format tab in the Properties view.

  2. Replace [General] in the Format box with ###0;###0;0;0.

    The fourth section in the mask causes a null value to be represented as zero.