# SQL Query help Inner Join in Script with two Databases

**URL:** <https://forum.inductiveautomation.com/t/sql-query-help-inner-join-in-script-with-two-databases/12509>\
**Category:** Archived Topics\
**Created:** [January 3, 2017, 4:14pm UTC](https://forum.inductiveautomation.com/t/sql-query-help-inner-join-in-script-with-two-databases/12509 "2017-01-03T16:14:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mives1](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@mives1](https://forum.inductiveautomation.com/u/mives1)\
**Post date:** [January 3, 2017, 4:14pm UTC](https://forum.inductiveautomation.com/t/sql-query-help-inner-join-in-script-with-two-databases/12509/1 "2017-01-03T16:14:02Z")

</div>

[code] qDowntimePDS = system.db.runPrepQuery(“SELECT dt.PlannedDT[Planned],sum(cast(DATEDIFF(ms,dt.begintime,dt.endtime) as int)) as Diff,pdt.ReasonCodeDescription[Reason] ,pdt.Note[Note],count(pdt.ReasonCodeDescription)[count] FROM DownTime dt inner join PROD\_DownTime\_Detail pdt on pdt.RefID=dt.ID where dt.runid=? and dt.PlannedDT = 0 AND pdt.SequenceDate BETWEEN ? AND ? group by dt.PlannedDT,pdt.ReasonCodeDescription,pdt.Note order by Diff desc”,[varRunID,startDateFormatted,endDateFormatted])

[/code]

Default Data Base for the Project name is MES.  
This Query worked until I seperated the data base and put Analysis seperate.  
“PROD\_DownTime\_Detail” is a schema in the Analysis database.  
“DownTime” is a Scema in the MES database.

So I know I need to define Analysis somewhere I think at the end of line,“Analysis”)  
But when you do this it can not find Downtime in the default database.

---

<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:** [January 8, 2017, 3:18am UTC](https://forum.inductiveautomation.com/t/sql-query-help-inner-join-in-script-with-two-databases/12509/2 "2017-01-08T03:18:03Z")

</div>

You cannot do this.  
A query runs in a single database connection. That database actually identifies the schemas and table names given in the query. You will have to use the database’s own features for connecting two databases together.
