# Combining Databases with Conditions

**URL:** <https://forum.inductiveautomation.com/t/combining-databases-with-conditions/88370>\
**Category:** Ignition\
**Tags:** database, perspective, scripting\
**Created:** [April 4, 2024, 6:51pm UTC](https://forum.inductiveautomation.com/t/combining-databases-with-conditions/88370 "2024-04-04T18:51:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![csilk](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@csilk](https://forum.inductiveautomation.com/u/csilk)\
**Post date:** [April 4, 2024, 6:51pm UTC](https://forum.inductiveautomation.com/t/combining-databases-with-conditions/88370/1 "2024-04-04T18:51:54Z")

</div>

I have two databases that I am trying to combine into a single table. I am trying to accomplish what is shown in table "dbA + dbB":

Basically, I would like "dbA + dbB" to populate based on the status in "dbB" equal to "Accepted". But also show when the time slots are empty.

 ![image_2024-04-04_143628782](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/4/94e57c3b8f6d83a648126da46276252ed3390cc9.png)

The code I am using is:

```auto
SELECT *
FROM dbA
left join dbB on dbA.timeSlot = dbB.timeSlot
where dbB.status = "Accepted"

```

And my code outputs:

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/3/030419d3331d9eb45fafe9e10b8cf6c5729f98fa.png)

I'm sure that there is a fairly simple answer, but I just cannot figure this out... Any help would be greatly appreciated.

Thank you.

---

<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 4, 2024, 6:55pm UTC](https://forum.inductiveautomation.com/t/combining-databases-with-conditions/88370/2 "2024-04-04T18:55:32Z")

</div>

You have to put the status check into a subquery, and left join against that instead of directly on table B.

(Terminology note: In English, you use "DB" for a collection of tables, not just one table.)

---

<div class="post-metadata">

**Author:** ![csilk](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@csilk](https://forum.inductiveautomation.com/u/csilk)\
**Post date:** [April 4, 2024, 7:01pm UTC](https://forum.inductiveautomation.com/t/combining-databases-with-conditions/88370/3 "2024-04-04T19:01:46Z")

</div>

So how would that be written? I understand the concept, just not sure how to code that. Also, thank you for the terminology tips.

---

<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 4, 2024, 7:08pm UTC](https://forum.inductiveautomation.com/t/combining-databases-with-conditions/88370/4 "2024-04-04T19:08:51Z")

</div>

Something like this:

```auto
SELECT tableA.TimeSlot, filteredB.Status, filteredB."First Name", filteredB.Last Name¨
FROM tableA
LEFT JOIN (
    SELECT *
    FROM tableB
    WHERE Status = 'Accepted'
) filteredB on tableA.TimeSlot = filteredB.TimeSlot

```

Note the explicit output columns. The general asterisk can cause problems when there are duplicate column names across a join.

---

<div class="post-metadata">

**Author:** ![csilk](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@csilk](https://forum.inductiveautomation.com/u/csilk)\
**Post date:** [April 4, 2024, 7:16pm UTC](https://forum.inductiveautomation.com/t/combining-databases-with-conditions/88370/5 "2024-04-04T19:16:54Z")

</div>

That works perfect, thank you!

---

<div class="post-metadata">

**Author:** ![csilk](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@csilk](https://forum.inductiveautomation.com/u/csilk)\
**Post date:** [April 5, 2024, 4:11pm UTC](https://forum.inductiveautomation.com/t/combining-databases-with-conditions/88370/6 "2024-04-05T16:11:27Z")

</div>

Alternatively, how could I structure a separate query to output available time slots? My plan is to then have a dropdown list that is bound to that query so that the user is able to schedule during an available time.

---

<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 5, 2024, 4:12pm UTC](https://forum.inductiveautomation.com/t/combining-databases-with-conditions/88370/7 "2024-04-05T16:12:53Z")

</div>

Add `WHERE filteredB.TimeSlot IS NULL` to the end of the query.
