How Nesting Works
Let's use a simple data relationship to help illustrate how nesting occurs. Imagine we have data collected from two unrelated sources that look the ones seen in this table.
We want to create a data source connecting all these things for reporting using nesting. The trick is to identify where the two datasources connect. You may notice in the data above The CodePK column in the Codes datasource matches up with the CodeID column of the Frequency datasource. This is where we will connect the two datasources. We will make our Codes datasource the parent and the Frequency the child. The queries would look something like below.
What this means for our resulting data is that the parent query is called first, and a set of results is returned to the parent query, named
Codes. After this data has been retrieved, the Child query will execute, once for each row of the parent, substituting the value of
CodePK into the child query where we have the
The resulting data will have a structure like this:
Each row returns the CodePK and Code from the parent query, but also the results of the child query, that apply to the
CodeID we get from the parent PK.
Now, in our Report Design, we will have access to our datasource
Frequency that linked these two sets of data together through a shared value.
Nested Queries are powerful and easy to use, but users should be aware of runtime implications. Imagine the scenario above, where we have two sets of data, each with 5000+ rows. When our child query executes, each row of its query is going to require a lookup from the parent. For most common sets of data and database sizes, this won't be an issue, but it's possible to imagine that instead of just one child query, we have a dozen.
In addition, some of those children also have many children. It's very easy to see in this scenario how exponential growth occurs and our system performance may suffer. Most report designers will limit query sizing as oversized data structures are simply not as easy to work with. However, if you feel an urge to generate massive complex trees of million line queries, you may be waiting a while.
Nested Query Example - Equipment Downtime
Here in this example, we have two database tables: an equipment table, which contains all of the equipment we have, as well as a downtime table, which contains a list of every downtime event. The tables are put together like the tables below.
- Create two tables in the database that are similar to the tables listed above.
- Open a report, and navigate to the Data Panel.
- Click the Plus
Change the Data Key property to Equipment.
Type the query below into the query area. Here we are pulling out three columns of our equipment table, and giving each column a more descriptive name. Pay close attention to the spelling of the aliased names (names following the AS keywords), as we will have to reference EquipmentIDNumber later in our subquery.
- On the right of the Nested Query section, click the Plus
Select the sub_query, and rename the Data Key property to rename it. In this example, we will use the name EquipDowntime.
Type the following query into the query area.
Here we pull in our downtime table, but we only need the cause and minutes_down, since we are already grabbing the Equpiment_id from the first query. The WHERE clause is where we link this query to the parent with the equipment ids.
In the Parameter 1 field, type the following:
Note that we are directly referencing one of the aliased column names from the Equipment query.
That is all it takes to make a Nested Query! We now have two separate tables being called and linked together by the equipment id.
To check that your queries worked, go to the Preview tab and look at the XML data that comes up. You should have a data set inside (indented) each row of Equipment data.