# Query tag historian data using generic SQL query

**URL:** <https://forum.inductiveautomation.com/t/query-tag-historian-data-using-generic-sql-query/82169>\
**Category:** Ignition\
**Created:** [November 21, 2023, 4:48am UTC](https://forum.inductiveautomation.com/t/query-tag-historian-data-using-generic-sql-query/82169 "2023-11-21T04:48:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaoliang](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chaoliang](https://forum.inductiveautomation.com/u/chaoliang)\
**Post date:** [November 21, 2023, 4:48am UTC](https://forum.inductiveautomation.com/t/query-tag-historian-data-using-generic-sql-query/82169/1 "2023-11-21T04:48:12Z")

</div>

I want to query the historian data using (tagName, startTime, endTime) and send the result out as Json file.

I thought abut using named query, but it does not support direct querying the historian tag list.

Then I am thinking about writing query by joining the multiple tag historian data tables to get the result.

Here's the information I found about the tag historian data tables:  
[https://docs.inductiveautomation.com/display/DOC81/Ignition+Database+Table+Reference](https://docs.inductiveautomation.com/display/DOC81/Ignition+Database+Table+Reference)

I can try and test out the correct query script for the tag historian, but just wonder whether there's already some premade ignition query I can use straightaway?

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/4/f4fd708e03a43c4dfff17eddf588fd2a87d0bd3f.png)

---

<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:** [November 21, 2023, 12:36pm UTC](https://forum.inductiveautomation.com/t/query-tag-historian-data-using-generic-sql-query/82169/2 "2023-11-21T12:36:11Z")

</div>

> [@chaoliang](#):
>
> premade ignition query

The tag history `system.tag.*` calls are the "premade" query.

Fundamentally, you will be search for the set of tag ids that correspond to your tag path, and the set of partitions that correspond to your time range. Then you will construct a query that unions those partitions together after filtering each for the desired tag and time.

There isn't a single generic query that can do this.

---

<div class="post-metadata">

**Author:** ![chaoliang](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chaoliang](https://forum.inductiveautomation.com/u/chaoliang)\
**Post date:** [November 21, 2023, 11:56pm UTC](https://forum.inductiveautomation.com/t/query-tag-historian-data-using-generic-sql-query/82169/3 "2023-11-21T23:56:48Z")

</div>

Thanks for the reply.

I was testing the following query yesterday.

# [system.tag.queryTagHistory](https://docs.inductiveautomation.com/display/DOC79/system.tag.queryTagHistory)

It's working if to set the "start time, end time and interval time", but I couldn't figure out how to return only a fixed amount of data points.  
For example I want to return only 200 data point, doesn't matter whether it's duration of 1 day or 1 month, for the 200 data point, I can either choose average or last value.

I found the following statement in the doc, but I don't know whether it's the same meaning of "fixed size" as I want.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/1/41c7205be2b951c8b9ea1a43ffdbfe6a7e3e7ad7.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 22, 2023, 1:01am UTC](https://forum.inductiveautomation.com/t/query-tag-historian-data-using-generic-sql-query/82169/4 "2023-11-22T01:01:55Z")

</div>

queryTagHistory literally has a returnSize parameter, documented in the manual page you linked.

---

<div class="post-metadata">

**Author:** ![chaoliang](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chaoliang](https://forum.inductiveautomation.com/u/chaoliang)\
**Post date:** [November 22, 2023, 4:58am UTC](https://forum.inductiveautomation.com/t/query-tag-historian-data-using-generic-sql-query/82169/5 "2023-11-22T04:58:20Z")

</div>

Thanks.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/4/34b0f0eeac85823a1b5cd01533c1c0c770cec0dc.png)

I have just tested it, when set the returnSize to 10, it will generate 10 results, regardless whether the intervalSeconds settings.
