# Combining Nested Query with External Scripted Dataset

**URL:** <https://forum.inductiveautomation.com/t/combining-nested-query-with-external-scripted-dataset/105670>\
**Category:** Ignition\
**Tags:** ignition81, reporting\
**Created:** [April 29, 2025, 10:54am UTC](https://forum.inductiveautomation.com/t/combining-nested-query-with-external-scripted-dataset/105670 "2025-04-29T10:54:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![raro](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@raro](https://forum.inductiveautomation.com/u/raro)\
**Post date:** [April 29, 2025, 10:54am UTC](https://forum.inductiveautomation.com/t/combining-nested-query-with-external-scripted-dataset/105670/1 "2025-04-29T10:54:22Z")

</div>

Hi everyone,

I'm working on a somewhat unusual report setup

current setup:  
I have two working data sources:

**1. A multi-level Nested Query**  
This retrieves structured data from MSSQL in the following hierarchy:

```python
Recipe
  └── Operation
      └── Group
          └── Step

```

It renders correctly in the report using Table Groups (parent/child/child) like:

- Operation 1
  - Group 1
    - Step 1 - value

  - Group 2
    - Step 1 - value

- Operation 2
  - Group 1
    - Step 1 - value
    - Step 2 - value
    - Step 3 - value

  - Group 2
    - Step 1 - value

So far, this is working great.

**2. A script-based data source**  
This pulls a dataset from a different SQL database (external table) using a parameter like batch number.  
This also works fine, but the result can only be displayed separately in the report. Using second table.

Now I want to insert the external table inline into the nested report, based on conditions found in specific steps.

For example:

- If a specific `IdStepValue = 300` I want to display the external table directly beneath that step.\*

```python
Operation 2
  Group 1
    Step 1 - value
    Step 2 - [INSERT EXTERNAL TABLE HERE]
    Step 3 - value

```

The external table structure is always consistent, and I know exactly where it should be inserted based on the values in a specific step. However, this external table cannot be joined directly into the existing nested query structure.

So my idea was to combine both data sources inside a scripting data source, manually transform everything into one unified nested dataset, and feed it to the report as a single source.  
This would allow me to use child tables in a table group structure and conditionally embed the external table under the correct step.

I found this old post which shows how to build `ReportQueryResults` / `ReportQueryRow` manually:

> [@Report with Scripted Nested Data](https://forum.inductiveautomation.com/t/report-with-scripted-nested-data/11801/25):
>
> Yes, that was it. Variable scoping error on my part. Thanks!

While I understand the principle, it looks fairly complex when dealing with multiple nested levels.

So my question would be:

- Is it realistically possible to do this cleanly via script (merging nested query + scripted external table)?
- Is there a recommended way to dynamically inject another table into a nested query layout?
- Is there any other way?

br,

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 29, 2025, 11:25am UTC](https://forum.inductiveautomation.com/t/combining-nested-query-with-external-scripted-dataset/105670/2 "2025-04-29T11:25:06Z")

</div>

> [@raro](#):
>
> it looks fairly complex

Yes, somewhat.

1. Cleanly? You have to re-construct the whole tree. Sorry.

2. Just this.

3. Not that I know of.
