# Filter Table Based on Data in different DB source

**URL:** <https://forum.inductiveautomation.com/t/filter-table-based-on-data-in-different-db-source/97229>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [October 14, 2024, 4:09pm UTC](https://forum.inductiveautomation.com/t/filter-table-based-on-data-in-different-db-source/97229 "2024-10-14T16:09:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nate\_macs](https://avatars.discourse-cdn.com/v4/letter/n/b5ac83/32.png) [@nate\_macs](https://forum.inductiveautomation.com/u/nate_macs)\
**Post date:** [October 14, 2024, 4:09pm UTC](https://forum.inductiveautomation.com/t/filter-table-based-on-data-in-different-db-source/97229/1 "2024-10-14T16:09:40Z")

</div>

Hello,

I have a perspective table bound to a named query referencing one DB connection (DB1). Users write additional data against keys from named query and the written data is stored in a different DB connection (DB2) - ie the newly written data is not accessible via the named query. Users would like to filter the table by data they are writing to DB2.

Imagine DB1 contains:  
key1, property1, property,2

DB2 contains:  
key1, property 3

How can I filter or otherwise reference property 3 while using a table bound to a query referencing DB1?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [October 14, 2024, 4:53pm UTC](https://forum.inductiveautomation.com/t/filter-table-based-on-data-in-different-db-source/97229/2 "2024-10-14T16:53:58Z")

</div>

How are you displaying this information to the user? Why not join DB1 and DB2 data sources in your query and display both in one table, or are you already doing that?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [October 14, 2024, 4:57pm UTC](https://forum.inductiveautomation.com/t/filter-table-based-on-data-in-different-db-source/97229/3 "2024-10-14T16:57:34Z")

</div>

> [@nate\_macs](#):
>
> How can I filter or otherwise reference property 3 while using a table bound to a query referencing DB1?

You will have to do the join and filter manually in a script. Or if you are able to, use @pturmel's

> [@Automation Professionals' Integration Toolkit Module](https://forum.inductiveautomation.com/t/automation-professionals-integration-toolkit-module/74934):
>
> Automation Professionals is pleased to announce the immediately availability of a major upgrade to its free Simulation Aids Integration Toolkit module. The new feature justifying the "major upgrade" language is the implementation of eleven thirty-one new expression functions which use iteration to accomplish complex transforms of datasets and lists, to or from other datasets and lists, applying filtering, ordering, and computed new columns along the way. While there are certain operations that…

to do it in an expression.

---

<div class="post-metadata">

**Author:** ![nate\_macs](https://avatars.discourse-cdn.com/v4/letter/n/b5ac83/32.png) [@nate\_macs](https://forum.inductiveautomation.com/u/nate_macs)\
**Post date:** [October 14, 2024, 5:01pm UTC](https://forum.inductiveautomation.com/t/filter-table-based-on-data-in-different-db-source/97229/4 "2024-10-14T17:01:51Z")

</div>

I'm displaying via table component with data property bound to a named query. The named query is using DB1 as a database connection and referencing a table within DB1. The users are writing to a different table in a different database connection in DB2.

Can I still do a SQL join in the named query if I try to join on a table in DB2 when the database connection referenced is DB1?

My other idea was scripting a transform...

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [October 14, 2024, 5:04pm UTC](https://forum.inductiveautomation.com/t/filter-table-based-on-data-in-different-db-source/97229/5 "2024-10-14T17:04:05Z")

</div>

> [@nate\_macs](#):
>
> Can I still do a SQL join in the named query if I try to join on a table in DB2 when the database connection referenced is DB1?

If DB1 and DB2 are the same db flavor, that is both MySQL or both postgres etc, then yes you can join them in a single query. If one is MySQL and one postgres, then no.

In that case, I highly recommend the Integration Toolkit that @lrose linked to. This toolkit allows you to gain a lot of performance by using an expression over a script transform.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [October 14, 2024, 5:07pm UTC](https://forum.inductiveautomation.com/t/filter-table-based-on-data-in-different-db-source/97229/6 "2024-10-14T17:07:53Z")

</div>

> [@dkhayes117](#):
>
> If DB1 and DB2 are the same db flavor, that is both MySQL or both postgres etc, then yes you can join them in a single query

I don't think that is true, they are different _connections_ implying different databases. Doing a cross connection join, at the very least isn't supported from a Named Query. You could potentially do it in a stored procedure, assuming that both Databases are located in the same Database server, but again. Not from a named query, named queries only support a single connection.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [October 14, 2024, 5:11pm UTC](https://forum.inductiveautomation.com/t/filter-table-based-on-data-in-different-db-source/97229/7 "2024-10-14T17:11:00Z")

</div>

> [@lrose](#):
>
> I don't think that is true

~~You absolutely can do it in a named query, I do it all the time with MariaDB~~. One database schema, is the right terminology?, with mulitple databases using a database connection for each database (in my case).

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [October 14, 2024, 5:13pm UTC](https://forum.inductiveautomation.com/t/filter-table-based-on-data-in-different-db-source/97229/8 "2024-10-14T17:13:38Z")

</div>

> [@dkhayes117](#):
>
> You absolutely can do it in a named query

How do you get a single named query to reference a second ignition connection? Without actually seeing the query, I can't say for sure, but this feels like it is venturing into the neighborhood of a SQL script.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [October 14, 2024, 5:15pm UTC](https://forum.inductiveautomation.com/t/filter-table-based-on-data-in-different-db-source/97229/9 "2024-10-14T17:15:56Z")

</div>

db connection called `cutting` and db connection called `staging`. Set default connection to `cutting`

```python
select *
from cut_history c
left join staging.stage_history s
    on s.serial = c.serial
where
...

```

EDIT  
You are probably right that the DBs would have to be on the same server/schema as it's probably not acutally using the other db connection in the query. It works because the databases live in the same place and is using the default connection only.
