Summarizing by group and subgroup

2019-06-12 10:02发布

问题:

I have a crystal report, it has a group by statement based on two fields

  1. Companyname
  2. Account Type

Now Crystal report work and it does group by "companyname" but it does not group by "account type". I have two types of account, "Regular and Premium". One company has both type of account with us and it pulls on regular and not premium. What do you think is the issue.

I am new to crystal report. I am familiar with SQL Though. The differences that I see is

  • There is no aggregate function used for any column - in SQL an aggregate field is required to get meaningful result
  • There are about 10 fields in the report but group by is used only on two fields - in SQL you have to group by all fields if there are more than two fields
  • The group by fields are in the middle and in end - again in SQL the order of groupby field is important

In this case I am grouping by companyname first and then account status. Any insight will be helpful.

Note that I generated the equivalent SQL statement from Crystal Report. That query was ok except there were not group by statements. I added those and the aggregate column myself and I do get the same result as I am getting in Crystal (but the SQL report is more accurate as it does group by both the fields not just one).

Edit: Example Date. Each company can have multiple account of the same type. I want aggregate on "Company Name" and "AccountType" so that the data is listed as follow.

+----+--------------+-------------+------+
| ID | Company Name | AccountType | Sale |
+----+--------------+-------------+------+
| 1  | ABC          | I           | 500  |
| 2  | ABC          | I           | 600  |
| 3  | ABC          | O           | 1000 |
| 4  | ABC          | O           | 2000 |
| 5  | ABC          | O           | 3000 |
| 6  | XYZ          | O           | 2500 |
| 7  | LMN          | O           | 3400 |
+----+--------------+-------------+------+

Output I want from the above table is

+--------------+-------------+------+
| Company Name | AccountType | Sale |
+--------------+-------------+------+
| ABC          | I           | 1100 |
| ABC          | O           | 6000 |
| XYZ          | O           | 2500 |
| LMN          | O           | 3400 |
+--------------+-------------+------+

回答1:

Update to incorporate comment discussion and revised question:

There are two basic options for resolving this issue:

1) Revise the SQL to perform the desired aggregation.

2) Within Crystal, add two groups, one for the company, then one for the account type.

Before you add groups in crystal, you have several standard sections, including the Report Header, Page Header, Details, Report Footer, and Page Footer.

If you do not perform grouping, each row that is read in the database will be displayed in the details section (technically, the entire details section is repeated for each row).

When you add the first group (Company), Crystal adds a Group Header #1 before the Details section and a Group Header #1 after the Details section.

If you run the report at this point, for each company Crystal will show the Group Header before each group of detail records associated with that company, then will show all of the detail records for that company, and finally, will shown the Group footer for that company.

Typically, the group header is used to display common information for that grouping that does not need to be repeated for each detail record. In this example, we could display the company name and other information related to the company.

Likewise, the group footer is typically used to display summary information for all of detail records displayed within that group.

In this case, we could add an aggregate that would summarize the Sale amount, which would be the total sales for that company, regardless of the account type.

When the second group is added, it will perform sub-grouping on the original group.

When the second group is added, Crystal will place a Group Header #2 below Group Header #1 and above the Details and will place a Group Footer # 2 directly below the details and above the Group Footer #1.

At this point, you have a report format similar to the following:

Group Header #1 (Company)
  Group Header #2 (Account Type)
    Details (the individual sale records)
  Group Footer #2
Group Header #1

In this case, for each company, we want to group the details records by account type. So we can add information that describes the account type, if we want, to the Group Header #2 and we can add aggregates to Group Footer #2 to display totals for each account type within the company.

Now, if all that is desired is to show the totals for each account type within each company, then the only section that we need to show in the report is Group Footer #2. All of the fields (company, account type, sale aggregate) are available in this Footer, so we don't need any of the additional areas.