Skip to Content

Show All Sub-Groups and a Summary for Every Group

If you’ve ever created a report that has several groups, for example Customer, Year, & Product, you may notice that there may be missing groups or sub-groups. For example you may not see groups for all Products in a certain Year for a particular Customer. However, you need to have summary values for all Products for each Year for every Customer…even if the sub-total is just a zero. This issue may affect any charts or cross-tabs you have on your report.

Here’s an example of the data you may see:


Here’s an example of the data that you may want to see:


The reason why you don’t have all of the sub-groups and all of the summaries in your report is that the data doesn’t exist in your database. If Customer A has never purchased Product 1 in years 2012 or 2010, then your report isn’t going to show those groups or sub-totals. If Customer A has never purchase Product 2, then a Customer level cross-tab will not have a column for Product 2.

If it’s really important to you that each group show all sub-groups and at least a zero for a sub-total, then there are workarounds. One solution is to “roll up” data in a main report and passing this rolled up data to a subreport that brings back all sub-groups. This technique is covered in a blog here. The technique that is covered in this blog though is to add a subreport to your report based on a Command object that will create all needed records. You will need to be able to write your own SQL syntax in the Command object to use this technique.

1) First, start by opening up the sample report located here. Note that the report consists of a main report and a sub-report. The main report is used to bring back any Customers that we need to show. The subreport will be used to bring back all of the Customer Orders by Product and by Year.

2) Now go to the Database menu > Show SQL Query. You may be prompted to set the database location to view the query, so just set the database to any ODBC connection that you have. Or just view the main report’s query below…


`Customer`.`Customer Name`,

`Product`.`Product Name`,

`Orders`.`Order Amount`,

`Customer`.`Customer ID`,

`Customer`.`Country`, `Orders`.`Order Date`


((`Customer` `Customer`

INNER JOIN `Orders` `Orders` ON `Customer`.`Customer ID`=`Orders`.`Customer ID`)

INNER JOIN `Orders Detail` `Orders_Detail` ON `Orders`.`Order ID`=`Orders_Detail`.`Order ID`)

INNER JOIN `Product` `Product` ON `Orders_Detail`.`Product ID`=`Product`.`Product ID`


(`Customer`.`Country`=’Canada’ OR `Customer`.`Country`=’France’)


`Customer`.`Customer Name`

3) Note that if you had the Xtreme sample database and pasted this query into a query analyzer against that database, you would see that a lot of Customers were missing a lot of products and dates.

4) Now right click on the Subreport and choose Edit Subreport. Go to the Database menu > Show SQL Query. The query below is what is used to ensure that every Customer has a line for every Product and year combination and a sub-total for Orders.

`ProductA`.`Product Name`,
{fn YEAR(`OrdersA`.`Order Date`)} AS OrderYear,

SUM(`Orders`.`Order Amount`)

((`Customer` `Customer`
INNER JOIN `Orders` `Orders` ON `Customer`.`Customer ID`=`Orders`.`Customer ID`)
INNER JOIN `Orders Detail` `Orders_Detail` ON `Orders`.`Order ID`=`Orders_Detail`.`Order ID`)
INNER JOIN `Product` `Product` ON `Orders_Detail`.`Product ID`=`Product`.`Product ID`

`Customer`.`Customer ID` =  {?CustomerID}
AND `Product`.`Product Name` = `ProductA`.`Product Name`
AND {fn YEAR(`Orders`.`Order Date`)} = {fn YEAR(`OrdersA`.`Order Date`)}
) AS sumOrderAmount

(`Orders` `OrdersA`
INNER JOIN `Orders Detail` `Orders_Detail` ON `OrdersA`.`Order ID`=`Orders_Detail`.`Order ID`)
INNER JOIN `Product` `ProductA` ON `Orders_Detail`.`Product ID`=`ProductA`.`Product ID`

So what is this SQL query actually doing?

1) The first line in the SELECT brings back all distinct Products.

2) The second line ensures brings back all distinc Year values.

3) The third line to the twelfth line is a sub-query which brings back a value for Orders based on the Product and the Year. Using a main query plus a sub-query will ensure that a sub-total of 0 will be returned for situations where a Product did not have any Orders for a particular Year.

4) Note that the only filter on the data (for the Customer ID) is in the above-mentioned sub-query. We don’t want to filter the main query as we want to ensure that a line of data is brought back for each Customer > Product > Year > Orders sub-total.

Please note that the above SQL syntax is based off of the Xtreme sample database and MS Access. The SQL syntax that you will need to write will vary depending on what your database client is. Please consult your database online help for syntax.

I hope that you find this tecnhique helpful. If you are looking for any other solutions or workarounds for Crystal Reports, please see my blog here.

Be the first to leave a comment
You must be Logged on to comment or reply to a post.