# Query Internal Tag Historian

**URL:** <https://forum.inductiveautomation.com/t/query-internal-tag-historian/59749>\
**Category:** Ignition\
**Tags:** ignition81, database\
**Created:** [May 18, 2022, 8:43pm UTC](https://forum.inductiveautomation.com/t/query-internal-tag-historian/59749 "2022-05-18T20:43:21Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [May 18, 2022, 8:43pm UTC](https://forum.inductiveautomation.com/t/query-internal-tag-historian/59749/1 "2022-05-18T20:43:21Z")

</div>

How do I query the internal tag historian SQLite database, if possible? I have tag history enabled for a flow and would like to use a query to do a summation of the last 7 days’ final flows.

What is the internal .db file location?

---

<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:** [May 18, 2022, 8:47pm UTC](https://forum.inductiveautomation.com/t/query-internal-tag-historian/59749/2 "2022-05-18T20:47:52Z")

</div>

Use, a Historical Tag Binding, or `system.tag.queryTagHistory` on the correct provider.

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [May 18, 2022, 8:56pm UTC](https://forum.inductiveautomation.com/t/query-internal-tag-historian/59749/3 "2022-05-18T20:56:32Z")

</div>

What about Vision?

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [May 18, 2022, 8:56pm UTC](https://forum.inductiveautomation.com/t/query-internal-tag-historian/59749/4 "2022-05-18T20:56:41Z")

</div>

I want to learn more about this too if examples are povided

---

<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:** [May 18, 2022, 9:00pm UTC](https://forum.inductiveautomation.com/t/query-internal-tag-historian/59749/5 "2022-05-18T21:00:00Z")

</div>

I don’t know about bindings in vision, but `system.tag.queryTagHistory` and `queryTagCalculations` should be available.

@zacharyw.larson What do you want to know ?

---

<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:** [May 18, 2022, 9:16pm UTC](https://forum.inductiveautomation.com/t/query-internal-tag-historian/59749/6 "2022-05-18T21:16:39Z")

</div>

In this instance it is the same for either perspective or vision.

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [May 19, 2022, 2:57am UTC](https://forum.inductiveautomation.com/t/query-internal-tag-historian/59749/7 "2022-05-19T02:57:27Z")

</div>

Is this something like what you are looking for?

```auto
endTime = system.date.now()
startTime = system.date.addDays(endTime, -7)
dataSet = system.tag.queryTagHistory(paths=['[default]New Tag4'], startDate=startTime, endDate=endTime)
sumFlow = 0
for row in range(dataSet.getRowCount()):
	sumFlow += dataSet.getValueAt(row,1)
print sumFlow

```

---

<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:** [May 19, 2022, 10:07am UTC](https://forum.inductiveautomation.com/t/query-internal-tag-historian/59749/8 "2022-05-19T10:07:01Z")

</div>

> [@josborn](#):
>
> ```auto
> dataSet = system.tag.queryTagHistory(paths=['[default]New Tag4'], startDate=startTime, endDate=endTime)
> sumFlow = 0
> for row in range(dataSet.getRowCount()):
> sumFlow += dataSet.getValueAt(row,1)
> 
> ```

`queryTagCalculations` seems like an easier way to do this:

```python
dataset = system.tag.queryTagCalculation(paths, rangeHours=-24*7, calculations=["Sum"])
sumFlow = dataset.getValueAt(0, 1)

```

With the added benefit that you can query multiple calculations at the same time quite easily:

```python
dataset = system.tag.queryTagCalculation(paths, rangeHours=-24*7, calculations=["Sum", "Average"])
sumFlow = dataset.getValueAt(0, 1)
averageFlow = dataset.getValueAt(0, 2)

```

---

<div class="post-metadata">

**Author:** ![amarks](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/amarks/32/45610_2.png) [@amarks](https://forum.inductiveautomation.com/u/amarks)\
**Post date:** [May 19, 2022, 1:49pm UTC](https://forum.inductiveautomation.com/t/query-internal-tag-historian/59749/9 "2022-05-19T13:49:03Z")

</div>

@josborn @pascal.fragnoud

I actually ended up doing it with a couple of different parameters and bindings.

1. Parameter “LastNight” Expression binding: `addMinutes(midnight(now()),-1)`
2. Parameter “WeekAgo” Expression binding `addWeeks({LastNight},-1)`
3. Parameter “LastWeek\_Data” Tag History binding using historical date range and the two dates calculated in 1 and 2 and a fixed sample size interval of 24 hours.
4. Parameter “LastWeek\_Total” Expression binding `sum({LastWeek_Data},'output')`

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [May 19, 2022, 2:42pm UTC](https://forum.inductiveautomation.com/t/query-internal-tag-historian/59749/10 "2022-05-19T14:42:46Z")

</div>

> [@pascal.fragnoud](#):
>
> @zacharyw.larson What do you want to know ?

Well I made a transaction group for infeed count, outfeed count, and machine state for example.  
I can make a query of that table.

but is there a faster way to query the historian instead?  
I think to be much faster with getting data from the historian rather than transaction groups and queries is what I meant I wan to learn.

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [May 19, 2022, 4:03pm UTC](https://forum.inductiveautomation.com/t/query-internal-tag-historian/59749/11 "2022-05-19T16:03:52Z")

</div>

> [@pascal.fragnoud](#):
>
> seems like an easier way to do this

To each their own, this was not "hard" by any means and allows you to manipulate the dataset in any way you want.

---

<div class="post-metadata">

**Author:** ![Steve\_Laubach](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steve_laubach/32/60900_2.png) [@Steve\_Laubach](https://forum.inductiveautomation.com/u/Steve_Laubach)\
**Post date:** [May 19, 2022, 4:13pm UTC](https://forum.inductiveautomation.com/t/query-internal-tag-historian/59749/12 "2022-05-19T16:13:41Z")

</div>

The 2 methods they’re talking about are ways to query the historical data collection. I presume you’re counting in the PLC and collecting the count on some interval with a SQL Bridge transaction. You are probably doing it in a good way for what you’re trying to accomplish.

You could use the historical collection if you were doing historic collection of the state of a bit that goes high every time you get a count but I would prefer the SQL Bridge method because you are less likely to miss a count when the counting is happening on the PLC and you’d be recording fewer records to record the same data. It is good to consolidate round trips to the database and to get your data in real time (PLC) vs through a comms driver.
