Complex Totals Query--Calculates Percentages and Performs Crosstab

Sales Representative Percentages "Drops" Example

January 28th 2003
home

Also see: Total Queries: Showing Those With 0 Count Other Total Queries

This example calculates how many contacts are converted into sales--which are sold by the sales representative. There are apartments and subdivisions contacted as potential customers and entered into a database; when any of them are converted into sales, a field called "video_sold_by" has the value entered "Sales Representative" or "Customer Service." Otherwise, the value (as manipulated later in the query) is "no sale." What needs to happen is, within a certain subdivision, all such attempts need to be totaled, and all which are designated with the "sales representative" need to be totaled, which then enables you to calculate a percentage.

Here is some sample data from the table:

tblMain
TEP Drop Datesubdivision_idRep IDVideo Sold ByDigital Sold ByInternet Sold By
15-Dec-02310   
11-Oct-0212  Customer Service
25-Mar-0225Customer Service  
5-Jun-0225Customer ServiceCustomer ServiceSales Representative
12-Mar-0235   
19-Feb-0235 Customer Service 
2-Apr-0235  Customer Service
10-May-0235Sales Representative  
28-May-042  Customer Service
17-Jul-0242 Customer Service  
5-Mar-0242 Sales Representative 
9-Sep-0242   
25-Jun-0242   
9-Jul-0242 Customer Service 
6-Mar-0251  Customer Service
29-Aug-0251   
14-May-0251Customer Service  
6-Mar-0251Customer ServiceCustomer Service 
30-Sep-0251Customer Service  
24-Jul-0251   
22-Mar-0251Customer Service  
6-Mar-0251   

Yes, the Rep ID field links up to the sales representative's name in another table tblRepresentatives (representative_name) and the subdivision_id links up to the subdivision's name in another table tblSubdivisions (subdivision_name).

In this example, the subdivision "Butterfield Ranch" has total sales ATTEMPTS of 84, and 8 of them were converted to sales by the sales representatives.

This required two queries. The first one, named qrySalesRepSalesPercentages, looked like this:

You have to add up all the entries, and you also have to add up only those with the value "sales representative" in the "video_sold_by" field (and also, with that value in the "high_speed_internet_sold_by" fields and "digital_sold_by" fields). The fields that do this are TotSales and VidCnt, especially (for handling the video sales portion).

Here is the syntax of the TotSales field which adds up all the entries:

TotSales: Count(IIf(IsNull([video_sold_by]),"nosale",[video_sold_by]))

This configures those with no entry in "video_sold_by" (which weren't sold by anyone) with the value "nosale" in there, and counts it as well. (I'm not sure if I need to do that; I inherited from earlier configurations of queries I was working on towards this end. (I am wondering if I could reconfigure this to read TotSales: Count(([video_sold_by])) and it would still work.)

Here is the syntax of the expression that adds up ONLY those with the value "sales representative" in the "video_sold_by" field:

VidCnt: Sum(IIf([video_sold_by]="Sales Representative",1,0))

The query returns results like this (only Butterfield Ranch shown, though this query returns all records by a certain representative:

TEP Drop DateRep IDVideo Sold ByRep Namesub NameTotSalesVidCnt
 2Sales RepresentativeMark DirtadianButterfield Ranch11
04-Mar-022 Mark DirtadianButterfield Ranch10
13-Mar-022Sales RepresentativeMark DirtadianButterfield Ranch11
18-Mar-022Customer ServiceMark DirtadianButterfield Ranch30
18-Mar-022Sales RepresentativeMark DirtadianButterfield Ranch11
20-Mar-022Customer ServiceMark DirtadianButterfield Ranch10
25-Mar-022Customer ServiceMark DirtadianButterfield Ranch20
25-Mar-022Sales RepresentativeMark DirtadianButterfield Ranch11
27-Mar-022 Mark DirtadianButterfield Ranch20

Again, all subdivisions are shown; this was only shortened for brievity.

For adding up sales rep. sales for "digital" and "high speed internet," you can actually place expressions for those in the same query near the end--following the same logic followed for VidCnt. Here are those expressions:

  • For high speed internet
    HSICnt: Sum(IIf([high_speed_internet_sold_by]="Sales Representative",1,0))
  • For digital
    DigCnt: Sum(IIf([digital_sold_by]="Sales Representative",1,0))

    The crosstab query totals everything--within each month, all values on VidCnt (for video) are added up (total sales rep sales), and all values from TotSales are added up within each month, and then they are divided to obtain the sales percentage.

    Here is a screenshot of the crosstab query:

    Created using Microsoft WORDPAD.