SkiBum
November 10th, 2009, 12:58 PM
I am struggling to figure out how to design what I think are basic calculations in Crystal Reports. I have read the CR User Guide and bought a reference book, and keep on failing to find the solution.
As an illustrative example, imagine the source database that I'm reporting from contains two tables: DEPARTMENT and EMPLOYEE. For this example, there is a one-to-many relationship: Each DEPARTMENT can have one or more EMPLOYEEs, but an EMPLOYEE must have one and only one DEPARTMENT. The EMPLOYEE table has a foreign key reference to the DEPARTMENT table.
At the top of my report, I want to summarize counts of different attributes for each table:
• Total DEPARTMENTS
• Number of DEPARTMENTS that are currently hiring.
• Number of DEPARTMENTS in the Main Street office.
• Total EMPLOYEES in all departments.
• Number of Male EMPLOYEES in all departments.
• Number of Female EMPLOYEES in all departments.
• Number of Telecommuting EMPLOYEES in all departments.
This would be very simple to do if I could just go into the Formula Workshop and create Count() or DistinctCount() functions with selective criteria. For example, DistinctCount({EMPLOYEE.TELECOMMUTE_FLAG} = ‘Y’). But I haven’t figured out how to make this work.
I can’t use Record Selection (Selection Expert), because I need the whole data set to count total departments and total employees. I can’t figure out how to make Group Selection work for me, because if I start grouping and subgrouping then a higher level group selection limits the number of records available to the subgroups. I also could not figure out how to use Summaries, because they don’t support selective criteria as well.
My solution (which doesn’t feel like the way I should do this) is that I’ve created numerous SQL queries within the Database Expert containing COUNT(*) and WHERE clauses to achieve these calculations. Intuitively this feels inefficient, because I’m querying the same databases over and over, instead of bringing the whole joined dataset into CR and performing the calculations within the report.
Having successfully achieved the top section of my report, I now want to list the following details for every department:
• DEPARTMENT.NAME
• Number of EMPLOYEES in the department.
• Number of Male EMPLOYEES in the department.
• Number of Female EMPLOYEES in the department.
• Number of Telecommuting EMPLOYEES in the department.
It seems obvious that I would group by DEPARTMENT.ID as the basis for each detail record. But then how do I “join” each of my individual calculation queries to get the details such as number of male employees in the Accounting department?? I have tried restructuring my “COUNT(*) / WHERE clause” queries into retrieving a record set using the same selection criteria that also contains the DEPARTMENT.ID, but when I join the results of this query to the original DEPARTMENT table, CR stops working.
I know that this is a lot of detail, but hopefully you get the gist of what I’m trying to accomplish. I really don’t know how to proceed.
Thanks,
Bill
As an illustrative example, imagine the source database that I'm reporting from contains two tables: DEPARTMENT and EMPLOYEE. For this example, there is a one-to-many relationship: Each DEPARTMENT can have one or more EMPLOYEEs, but an EMPLOYEE must have one and only one DEPARTMENT. The EMPLOYEE table has a foreign key reference to the DEPARTMENT table.
At the top of my report, I want to summarize counts of different attributes for each table:
• Total DEPARTMENTS
• Number of DEPARTMENTS that are currently hiring.
• Number of DEPARTMENTS in the Main Street office.
• Total EMPLOYEES in all departments.
• Number of Male EMPLOYEES in all departments.
• Number of Female EMPLOYEES in all departments.
• Number of Telecommuting EMPLOYEES in all departments.
This would be very simple to do if I could just go into the Formula Workshop and create Count() or DistinctCount() functions with selective criteria. For example, DistinctCount({EMPLOYEE.TELECOMMUTE_FLAG} = ‘Y’). But I haven’t figured out how to make this work.
I can’t use Record Selection (Selection Expert), because I need the whole data set to count total departments and total employees. I can’t figure out how to make Group Selection work for me, because if I start grouping and subgrouping then a higher level group selection limits the number of records available to the subgroups. I also could not figure out how to use Summaries, because they don’t support selective criteria as well.
My solution (which doesn’t feel like the way I should do this) is that I’ve created numerous SQL queries within the Database Expert containing COUNT(*) and WHERE clauses to achieve these calculations. Intuitively this feels inefficient, because I’m querying the same databases over and over, instead of bringing the whole joined dataset into CR and performing the calculations within the report.
Having successfully achieved the top section of my report, I now want to list the following details for every department:
• DEPARTMENT.NAME
• Number of EMPLOYEES in the department.
• Number of Male EMPLOYEES in the department.
• Number of Female EMPLOYEES in the department.
• Number of Telecommuting EMPLOYEES in the department.
It seems obvious that I would group by DEPARTMENT.ID as the basis for each detail record. But then how do I “join” each of my individual calculation queries to get the details such as number of male employees in the Accounting department?? I have tried restructuring my “COUNT(*) / WHERE clause” queries into retrieving a record set using the same selection criteria that also contains the DEPARTMENT.ID, but when I join the results of this query to the original DEPARTMENT table, CR stops working.
I know that this is a lot of detail, but hopefully you get the gist of what I’m trying to accomplish. I really don’t know how to proceed.
Thanks,
Bill