# Create Expression to filter Rows and Columns of SQL Query

**URL:** <https://forum.inductiveautomation.com/t/create-expression-to-filter-rows-and-columns-of-sql-query/58506>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [April 15, 2022, 3:40pm UTC](https://forum.inductiveautomation.com/t/create-expression-to-filter-rows-and-columns-of-sql-query/58506 "2022-04-15T15:40:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isaac\_Hodgkins](https://avatars.discourse-cdn.com/v4/letter/i/59ef9b/32.png) [@Isaac\_Hodgkins](https://forum.inductiveautomation.com/u/Isaac_Hodgkins)\
**Post date:** [April 15, 2022, 3:40pm UTC](https://forum.inductiveautomation.com/t/create-expression-to-filter-rows-and-columns-of-sql-query/58506/1 "2022-04-15T15:40:35Z")

</div>

Hey, I have a simple question regarding transforming a query. I have this SQL query shown below with 4 columns; tag, status, datetime, and minutes.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/8/d81a30ebf0cfe04d6cda3d5aecfb6406d494a0bf.png)

I want to run an expression on it so I can pick out a specific cell based on the tag and status. I know I can pick out a specific cell with {value}[0,1], but I’d like to search for minutes IF tag=“position 10 status” AND status=“Running”  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/6/e68d966e8e59be471eccb8963c89e12b7af559ec.png)

Thank you for the help!

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [April 15, 2022, 3:50pm UTC](https://forum.inductiveautomation.com/t/create-expression-to-filter-rows-and-columns-of-sql-query/58506/2 "2022-04-15T15:50:02Z")

</div>

There’s a module called SimulationAids that you can use to filter datasets with “pseudo-sql”. I recently started using it and its very easy. I don’t think there’s any built in way to do it via expressions. You’d either need to add WHERE clause parameters to your query that you modify, use python/jython to filter though that can get messy and would I would guess would be slower than the module, or you can use the free module.

> **[Ignition Third-Party Module Showcase](https://inductiveautomation.com/moduleshowcase/module/automation-professionals-llc-simulation-aids)**
>
> Easily extend Ignition's core functionality with Third-Party Modules. The Module Showcase is a great place to find a solution that best meets your needs.

---

<div class="post-metadata">

**Author:** ![Isaac\_Hodgkins](https://avatars.discourse-cdn.com/v4/letter/i/59ef9b/32.png) [@Isaac\_Hodgkins](https://forum.inductiveautomation.com/u/Isaac_Hodgkins)\
**Post date:** [April 15, 2022, 4:15pm UTC](https://forum.inductiveautomation.com/t/create-expression-to-filter-rows-and-columns-of-sql-query/58506/3 "2022-04-15T16:15:15Z")

</div>

Awesome! I’ll try this out!

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 15, 2022, 9:46pm UTC](https://forum.inductiveautomation.com/t/create-expression-to-filter-rows-and-columns-of-sql-query/58506/4 "2022-04-15T21:46:00Z")

</div>

This script transform might work for you. Try it in the script console.

```auto
d = [
  {"tag": "position 10 status", "status": "paused", "minutes": 2.27},
  {"tag": "position 10 status", "status": "Running", "minutes": 121.59},
  {"tag": "position 100 status", "status": "paused", "minutes": 0.06},
  {"tag": "position 100 status", "status": "Running", "minutes": 0.98},
  {"tag": "position 120 status", "status": "paused", "minutes": 122.93},
  {"tag": "position 120 status", "status": "Running", "minutes": 0.93},
  {"tag": "position 130 status", "status": "paused", "minutes": 1.23},
  {"tag": "position 130 status", "status": "Running", "minutes": 2.34},
  {"tag": "position 140 status", "status": "paused", "minutes": 3.45},
  {"tag": "position 140 status", "status": "Running", "minutes": 4.56}
]

filter(lambda row: row['tag'] == 'position 10 status' and row['status'] == 'Running', d)[0]['minutes']

```

For your binding transform you would use something like this:

```auto
# value is returned by the binding.
return filter(lambda row: row['tag'] == 'position 10 status' 
                     and row['status'] == 'Running', value)[0]['minutes']

```

I’m no Python guru. I got the technique on [Python list of dictionaries search - Stack Overflow](https://stackoverflow.com/questions/8653516/python-list-of-dictionaries-search) and there are a few other suggestions. Remember that Ignition is using Jython 2.7 so restrict your research to Python 2.7 solutions.

---

<div class="post-metadata">

**Author:** ![Isaac\_Hodgkins](https://avatars.discourse-cdn.com/v4/letter/i/59ef9b/32.png) [@Isaac\_Hodgkins](https://forum.inductiveautomation.com/u/Isaac_Hodgkins)\
**Post date:** [April 26, 2022, 8:06pm UTC](https://forum.inductiveautomation.com/t/create-expression-to-filter-rows-and-columns-of-sql-query/58506/5 "2022-04-26T20:06:42Z")

</div>

I was able to resolve this by making parameters in my Named Query. By making parameters I was able to filter the query by sensor and status to get a single cell.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/7/27cf4f30e566fc908f1912da19afcc826619c9d9.png)
