# Joining 2 query tags

**URL:** <https://forum.inductiveautomation.com/t/joining-2-query-tags/68226>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [January 4, 2023, 4:50pm UTC](https://forum.inductiveautomation.com/t/joining-2-query-tags/68226 "2023-01-04T16:50:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [January 4, 2023, 4:50pm UTC](https://forum.inductiveautomation.com/t/joining-2-query-tags/68226/1 "2023-01-04T16:50:05Z")

</div>

Good day all. Can you join 2 query tags? I know I can do it in the named query with a combinedDataset python code, only issue with this is the query take too long to do what I want in a timely manner. So, I have the query into a tag rather than a Named Query.

---

<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 4, 2023, 4:54pm UTC](https://forum.inductiveautomation.com/t/joining-2-query-tags/68226/2 "2023-01-04T16:54:25Z")

</div>

There's no native way to join (SQL-like I presume) datasets. You need jython. Consider an expression tag that hands off to runScript with the two other tags as arguments.

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [January 4, 2023, 5:07pm UTC](https://forum.inductiveautomation.com/t/joining-2-query-tags/68226/3 "2023-01-04T17:07:57Z")

</div>

Thats a good idea. Ill try to put something together and probably (more than likely) ask for input. LOL

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [January 4, 2023, 5:26pm UTC](https://forum.inductiveautomation.com/t/joining-2-query-tags/68226/4 "2023-01-04T17:26:57Z")

</div>

So here is how I was doing it previously (cant do it like this anymore due to IT restrictions):

```auto
def transform(self, value, quality, timestamp):
		
	styleAccountedFor = {'backgroundColor': "green"}
	MusterCall = value #system.db.runNamedQuery('MusterCall')
	SM_Salary = system.db.runNamedQuery('SM_Salary')
	combinedDataset = system.dataset.appendDataset(MusterCall,SM_Salary)
	combinedData = system.dataset.toPyDataSet(combinedDataset)
	    
	headers = system.dataset.getColumnHeaders(combinedData)
	        
	checkedInData = (self.getSibling("Table_0").props.data)
	accountedBadgeNum = [B['Badge_Num'] for B in checkedInData]
	            
	pyObj = []
	            
	for row in combinedData:
	    pyDict = {}
	    pyValueDict = {headers[i]: row[i] for i in range(len(row))}
	    if pyValueDict['ShawEmployeeID'] in accountedBadgeNum:
	        pyValueDict['isAccountedFor'] = True
	        pyDict['value'] = pyValueDict
	        pyDict["style"] = styleAccountedFor
	    else:
	        pyValueDict['isAccountedFor'] = False
	        pyDict['value'] = pyValueDict
	    pyObj.append(pyDict)
	        
	                    
	return pyObj

```

Here is how im tring to make an expression tag using the runScript:

```auto
runScript("MusterCall = system.db.runNamedQuery('MusterCall') SM_Salary = system.db.runNamedQuery('SM_Salary') combinedDataset = system.dataset.appendDataset(MusterCall,SM_Salary) combinedData = system.dataset.toPyDataSet(combinedDataset)")

```

Am I remotely close?

---

<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 4, 2023, 5:33pm UTC](https://forum.inductiveautomation.com/t/joining-2-query-tags/68226/5 "2023-01-04T17:33:10Z")

</div>

No, with `runScript`, you must choose between a one-line jython **expression** that returns your value, possibly involving scripts from your gateway scripting module, or a simple **function name** from your gateway scripting project, with arguments after the polling interval passed to that function.

So, something like this in the expression:

```auto
runScript("myScript.myFunction", 0, {[.]SomeTag}, {[.]SomeOtherTag})

```

That function would do the rest of the work.

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [January 4, 2023, 9:58pm UTC](https://forum.inductiveautomation.com/t/joining-2-query-tags/68226/7 "2023-01-04T21:58:00Z")

</div>

SO I have something that works for me:  
I have the Name query that took too long as a query tag and appended my other Named Query onto it like this:

```auto
	styleAccountedFor = {'backgroundColor': "green"}
	MusterCall = value
	SM_Salary = system.db.runNamedQuery('SM_Salary')
	combinedDataset = system.dataset.appendDataset(MusterCall,SM_Salary)
	combinedData = system.dataset.toPyDataSet(combinedDataset)
		    
	headers = system.dataset.getColumnHeaders(combinedData)
		        
	checkedInData = (self.getSibling("Table_0").props.data)
	accountedBadgeNum = [B['Badge_Num'] for B in checkedInData]
		            
	pyObj = []
		            
	for row in combinedData:
	    pyDict = {}
	    pyValueDict = {headers[i]: row[i] for i in range(len(row))}
	    if pyValueDict['ShawEmployeeID'] in accountedBadgeNum:
	        pyValueDict['isAccountedFor'] = True
	        pyDict['value'] = pyValueDict
	        pyDict["style"] = styleAccountedFor
	    else:
	        pyValueDict['isAccountedFor'] = False
	        pyDict['value'] = pyValueDict
	    pyObj.append(pyDict)
		        
		                    
	return pyObj

```

whole thing looks like this:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/7/b7384b30d87a97b2b6034afa27d894544e7524a0.png)

It mostly does what I want to.

My issue now is that the style is being applied very slow, as in lagging way behind. How can I speed that up? The style is applied when LH table sees the same data in RH table:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/6/86deea9f77a666bc9cc328cd5469e77ae1355155.png)

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [January 5, 2023, 9:45am UTC](https://forum.inductiveautomation.com/t/joining-2-query-tags/68226/8 "2023-01-05T09:45:57Z")

</div>

You could try with a query tag for each query and use an expression structure binding, instead of making one of the query in the transform.

PS: code style is good, but please get rid of all the extra whitespaces, it's triggering my OCD :X  
(also, the double quotes on line 22)

edit:  
Wait, where is that binding ? Is that on the table ?

---

<div class="post-metadata">

**Author:** ![mjohnson0238](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjohnson0238](https://forum.inductiveautomation.com/u/mjohnson0238)\
**Post date:** [January 6, 2023, 1:46pm UTC](https://forum.inductiveautomation.com/t/joining-2-query-tags/68226/9 "2023-01-06T13:46:49Z")

</div>

For anyone else wanting a solution I came up with a message handler that does the refreshBinding on the LH table when the RH tables data changes (change script). That way when my INSERT INTO python code populates the RH table a message tells the LH table to refresh its binding and and the style get applied.
