# Query from most recent partitionTag Historian Database

**URL:** <https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780>\
**Category:** Ignition\
**Created:** [July 24, 2023, 2:03pm UTC](https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780 "2023-07-24T14:03:03Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Babla\_Bogam](https://avatars.discourse-cdn.com/v4/letter/b/ecb155/32.png) [@Babla\_Bogam](https://forum.inductiveautomation.com/u/Babla_Bogam)\
**Post date:** [July 24, 2023, 2:03pm UTC](https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780/1 "2023-07-24T14:03:03Z")

</div>

I am querying the most recent partition table ([sqlt\_data\_1\_2023\_07]) from Tag Historian database. I plan to run this query programmatically but not sure how to keep track of the most recent partition. If data starts getting saved in \_08 partition in the future then this code will break at that time. Any suggestions on how to find the latest partition?

---

<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:** [July 24, 2023, 2:31pm UTC](https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780/2 "2023-07-24T14:31:56Z")

</div>

The architecture of Ignition's historian tables is documented here:

[https://docs.inductiveautomation.com/display/DOC81/Ignition+Database+Table+Reference](https://docs.inductiveautomation.com/display/DOC81/Ignition+Database+Table+Reference)

But, you should not be accessing these tables yourself. The **Right Answer™** is to use the provided `system.tag.query*` scripting functions and let Ignition manage its partitions however it wants. This protects you from breaking changes in the future.

---

<div class="post-metadata">

**Author:** ![Babla\_Bogam](https://avatars.discourse-cdn.com/v4/letter/b/ecb155/32.png) [@Babla\_Bogam](https://forum.inductiveautomation.com/u/Babla_Bogam)\
**Post date:** [July 24, 2023, 2:37pm UTC](https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780/3 "2023-07-24T14:37:11Z")

</div>

Thank you. I actually have a backend worker project in C# that primarily runs multiple background tasks (non-ignition related) but also wanted to add some ignition related task. So I wanted to access the data from ignition database for this ignition related task.

---

<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:** [July 24, 2023, 2:59pm UTC](https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780/4 "2023-07-24T14:59:22Z")

</div>

> [@Babla\_Bogam](#):
>
> I actually have a backend worker project in C# that

Any successor in your position will hate you. You should strongly consider doing your Ignition-related tasks in Ignition in a gateway event script.

---

<div class="post-metadata">

**Author:** ![Babla\_Bogam](https://avatars.discourse-cdn.com/v4/letter/b/ecb155/32.png) [@Babla\_Bogam](https://forum.inductiveautomation.com/u/Babla_Bogam)\
**Post date:** [July 24, 2023, 3:24pm UTC](https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780/5 "2023-07-24T15:24:52Z")

</div>

Thank you for the feedback. So if we have non ignition systems that need only few bits of information from ignition then is there any recommendation? It is not ignition task but an integration task that talks to multiple systems and want to also access specific ignition data, if possible.

---

<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:** [July 24, 2023, 3:28pm UTC](https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780/6 "2023-07-24T15:28:48Z")

</div>

The supported way for outside systems to get information from Ignition is to use the WebDev module to implement a web service. The jython scripts backing the web service can call Ignition's scripting API for any desired purpose (including writing **into** Ignition) without concern over Ignition's implementation details.

---

<div class="post-metadata">

**Author:** ![Babla\_Bogam](https://avatars.discourse-cdn.com/v4/letter/b/ecb155/32.png) [@Babla\_Bogam](https://forum.inductiveautomation.com/u/Babla_Bogam)\
**Post date:** [July 24, 2023, 3:32pm UTC](https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780/7 "2023-07-24T15:32:00Z")

</div>

That is very good option. Thank you

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [February 26, 2024, 2:05pm UTC](https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780/8 "2024-02-26T14:05:14Z")

</div>

Scripting is so slow though.

~~Is there not a way to use a named query and still get the data from the partitions?  
Or at least the most recent partition consistently?~~

I would prefer an alternative so I could leverage SQL speed without building my own tables.

---

<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:** [February 26, 2024, 2:21pm UTC](https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780/9 "2024-02-26T14:21:08Z")

</div>

> [@Zach\_Larson](#):
>
> I would prefer an alternative so I could leverage SQL speed without building my own tables.

How would you do this? Named queries are not the answer for everything, and you need some type of interface between ignition and the SQL server.

This isn't a trivial problem to solve.

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [February 26, 2024, 2:31pm UTC](https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780/10 "2024-02-26T14:31:59Z")

</div>

SELECT TOP (1) [pname] FROM [sqlth\_partitions] ORDER BY end\_time DESC

Gets you the latest partition, assuming you don't have multiple tag sources feeding into the same database.  
Then just use the result of that to pass the table name to any queries you want to execute.

Of course then you get into tag names, converting start\_time and end\_time to usable formats, managing retired tags, writing your own calculations for tags, etc etc.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [February 26, 2024, 2:44pm UTC](https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780/11 "2024-02-26T14:44:01Z")

</div>

My best guess, a tableID column instead of a time based table name.  
It is probably a terrible idea though with mandatory pruning implications.

I will use the scripting way.

---

<div class="post-metadata">

**Author:** ![keithms](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/keithms/32/58243_2.png) [@keithms](https://forum.inductiveautomation.com/u/keithms)\
**Post date:** [February 26, 2024, 2:47pm UTC](https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780/12 "2024-02-26T14:47:17Z")

</div>

There's no assurance that a custom solution will receive support or remain unaffected by unforeseen runtime intricacies. Future updates of Ignition may break it as well.

As long as execution speed of provided solutions are tolerable for your application the robustness they provide should be a priority.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [February 26, 2024, 2:48pm UTC](https://forum.inductiveautomation.com/t/query-from-most-recent-partitiontag-historian-database/76780/13 "2024-02-26T14:48:09Z")

</div>

I am not worried about that. The historian breaks on me in the reports.

Might not now that I am on 8.1.30 though.
