# Help getting query tag to select from desired timestamp

**URL:** <https://forum.inductiveautomation.com/t/help-getting-query-tag-to-select-from-desired-timestamp/111138>\
**Category:** Ignition\
**Tags:** ignition81, database, perspective\
**Created:** [October 1, 2025, 4:21pm UTC](https://forum.inductiveautomation.com/t/help-getting-query-tag-to-select-from-desired-timestamp/111138 "2025-10-01T16:21:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ian2](https://avatars.discourse-cdn.com/v4/letter/i/dec6dc/32.png) [@ian2](https://forum.inductiveautomation.com/u/ian2)\
**Post date:** [October 1, 2025, 4:21pm UTC](https://forum.inductiveautomation.com/t/help-getting-query-tag-to-select-from-desired-timestamp/111138/1 "2025-10-01T16:21:07Z")

</div>

Hi, I’m using a MySQL database connected to an Ignition **8.1.47** gateway and am having trouble with a Query tag inside of my perspective project.

This query tag is supposed to be equal to the value of another logged tag from 2 minutes ago so that I can compare the 2 values to measure rate of change. The query tag updates its values and is reading from the correct tag but only seems to be able to read from ~30 seconds ago, not 2+ minutes. The query is the following:

```python
SELECT   
   d.floatvalue AS value_numeric,   
   d.t_stamp AS source_tstamp
FROM sqlth_te te 
JOIN sqlt_data_1_2025_10 d ON d.tagid = te.id
WHERE te.tagpath LIKE '%AI_LIT02002/HMI/rActiveValue%' 
   AND d.t_stamp <= TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 MINUTE))
ORDER BY d.t_stamp DESC
LIMIT 1;

```

In this case, the tag whose historized value I want to read has the path AI\_LIT02002/HMI/rActiveValue.

From reading other forum posts, I suspect I am doing something wrong with DATE\_SUB, but I cannot figure out what.

The MySQL database and Ignition gateway are both hosted on the same machine.

Any help is appreciated!

---

<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 1, 2025, 4:28pm UTC](https://forum.inductiveautomation.com/t/help-getting-query-tag-to-select-from-desired-timestamp/111138/2 "2025-10-01T16:28:29Z")

</div>

Why are you querying the Historian tables directly instead of using the system functions?

I don't know why you're not getting the value from the time you expect, but if this is intended to be more than temporary, then it will fall apart once the historian moves into a different partition table.

---

<div class="post-metadata">

**Author:** ![ian2](https://avatars.discourse-cdn.com/v4/letter/i/dec6dc/32.png) [@ian2](https://forum.inductiveautomation.com/u/ian2)\
**Post date:** [October 1, 2025, 4:30pm UTC](https://forum.inductiveautomation.com/t/help-getting-query-tag-to-select-from-desired-timestamp/111138/3 "2025-10-01T16:30:30Z")

</div>

This was the first way I thought of to get the historized value from some time ago, what system functions are you referring to specifically?

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [October 1, 2025, 4:31pm UTC](https://forum.inductiveautomation.com/t/help-getting-query-tag-to-select-from-desired-timestamp/111138/4 "2025-10-01T16:31:06Z")

</div>

> **[system.tag.queryTagHistory | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-queryTagHistory)**
>
> Issues a query to the Tag Historian.

---

<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 1, 2025, 4:32pm UTC](https://forum.inductiveautomation.com/t/help-getting-query-tag-to-select-from-desired-timestamp/111138/5 "2025-10-01T16:32:58Z")

</div>

Also this:

> **[system.tag.queryTagCalculations | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-queryTagCalculations)**
>
> Queries various calculations (aggregations) for a set of tags over a specified range.

---

<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:** [October 1, 2025, 4:33pm UTC](https://forum.inductiveautomation.com/t/help-getting-query-tag-to-select-from-desired-timestamp/111138/6 "2025-10-01T16:33:12Z")

</div>

> [@ian2](#):
>
> `AND d.t_stamp <= TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 MINUTE))`

I suspect that the `TIMESTAMP()` function is not required.  
Anyway, the queryTagHistory function is the way to go.

---

<div class="post-metadata">

**Author:** ![robertm](https://avatars.discourse-cdn.com/v4/letter/r/ea5d25/32.png) [@robertm](https://forum.inductiveautomation.com/u/robertm)\
**Post date:** [October 1, 2025, 4:33pm UTC](https://forum.inductiveautomation.com/t/help-getting-query-tag-to-select-from-desired-timestamp/111138/7 "2025-10-01T16:33:24Z")

</div>

Or perhaps system.query.TagCalculations which can do the math for you.

> **[system.tag.queryTagCalculations | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-queryTagCalculations)**
>
> Queries various calculations (aggregations) for a set of tags over a specified range.

Edit: Irose quick on the draw 🙂

---

<div class="post-metadata">

**Author:** ![ian2](https://avatars.discourse-cdn.com/v4/letter/i/dec6dc/32.png) [@ian2](https://forum.inductiveautomation.com/u/ian2)\
**Post date:** [October 1, 2025, 4:44pm UTC](https://forum.inductiveautomation.com/t/help-getting-query-tag-to-select-from-desired-timestamp/111138/8 "2025-10-01T16:44:04Z")

</div>

Thank you all for the help! Will try QueryTagHistory and QueryTagCalculations and post results here.

---

<div class="post-metadata">

**Author:** ![ian2](https://avatars.discourse-cdn.com/v4/letter/i/dec6dc/32.png) [@ian2](https://forum.inductiveautomation.com/u/ian2)\
**Post date:** [October 22, 2025, 7:02pm UTC](https://forum.inductiveautomation.com/t/help-getting-query-tag-to-select-from-desired-timestamp/111138/9 "2025-10-22T19:02:38Z")

</div>

I ended up using a gateway script with queryTagHistory and got exactly what I wanted. Thank you all for the help!
