Dec 15, 2015

Sub Report in SSRS

In this article, I am going to demonstrate step by step example of creating Sub Report in SSRS.

Sub Reports are generally used when we need to show detail data with respect to summary data.

Example
Show Detail Data of Sales with respect to its Sales Header Data
  • Main Report will display Sales Header Data
  • Sub Report will display Sales Detail Data
Prerequisites
Basic steps to create Sub Report
  • Create a Main Report
  • Create a Sub report 
  • Apply Filter Parameter on Sub Report
  • Insert Sub Report into Main Report
  • Connect reports using Parameter(s)

Now lets have step by step example

Step 1: Create a Main Report
  • Create a connection with SQL Server. We would be using shared data source to fetch the data from the database for the report we created in an earlier article. Create a Shared Data Source
  • Right Click Reports>Add New Report, Report Wizard will appear to configure the report. Click Next

Add New Report

  • Configure Data Source. Select the Shared Data Source we created in Step 1 and Click Next

Select the Data Source

  • Design Query either thru Query Builder or write your own query and Click Next.
SELECT  SalesOrderID, OrderDate, SOH.CustomerID, P.FirstName+' '+P.LastName as CustomerName
        , SOH.SubTotal, SOH.TaxAmt, SOH.Freight,  SOH.TotalDue
FROM    Sales.SalesOrderHeader SOH
inner join Sales.Customer C on C.CustomerID = SOH.CustomerID
inner join Person.Person P on P.BusinessEntityID = C.PersonID
WHERE    SOH.OrderDate = @OrderDate

Design the Query

  • Select Tabular as Report Type and click Next.

Select the Report Type

  • You can configure to display the data based on grouping but as of now just click Finish.

Design the Table

  • Here, you can select Table Style. Default Style is Slate. Click Next

Choose the Table Style

  • At this stage, we are done with configuration of Main Report. Change the Report Name to DailySalesReport and Click Finish.

Set Report Name

Main Report

  • Query Parameter OrderDate that we used in the Query in Step 4. will be created automatically. Change the Data Type to Date/Time

Add Query Parameter


Step 2: Create a Sub Report

  • Create another report that would be used as Sub Report using the same step we used to create Main Report using below Query. Name the Report to SalesDeails.
SELECT  SOD.SalesOrderID, SalesOrderDetailID, P.Name
        , P.ProductNumber, SOD.UnitPrice, SOD.OrderQty
FROM    Sales.SalesOrderHeader SOH
inner join Sales.SalesOrderDetail SOD on SOH.SalesOrderID = SOD.SalesOrderID
inner join Production.Product P on SOD.ProductID = P.ProductID

Sub Report


Step 3: Add Filter Parameter to Sub Report

  • Add Parameter SalesOrderId to Sub Report.
  • Set Data type as Integer and Parameter visibility to Hidden as this would be automatically passed from Main Report to Sub Report.

Add Parameter

  • Right Click on Dataset>Datset Properties under Report Data

Dataset Properties

  • In Dataset Properties Windows select Filters Tab and Add Filter SalesOrderDetailId

Add Filter


Step 4: Insert Sub Report into Main Report

  • Add a group to the Data Section

Add Group

  • Merge all the cells


  • Right Click on the Group and select Insert>Subreport

Insert Sub Report

Insert Sub Report

  • Right Click SubReport Section > Sub Report Properties. In the pop window change the Name of Sub Report to SalesDetails and select DailySalesReport as below.

Name the Sub Report

Step 5: Connect reports using Parameter(s)

  • Select Parameters Tab and Add Parameter as below

Add Parameter

We are done with creating the Sub Report. Now lets run the report and see the output.


    Choose :
  • OR
  • To comment
No comments:
Write Comments