# Tag history in database-How to find tag names

**URL:** <https://forum.inductiveautomation.com/t/tag-history-in-database-how-to-find-tag-names/11802>\
**Category:** Ignition\
**Created:** [June 11, 2016, 5:37pm UTC](https://forum.inductiveautomation.com/t/tag-history-in-database-how-to-find-tag-names/11802 "2016-06-11T17:37:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sudhamoorthy](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sudhamoorthy](https://forum.inductiveautomation.com/u/sudhamoorthy)\
**Post date:** [June 11, 2016, 5:37pm UTC](https://forum.inductiveautomation.com/t/tag-history-in-database-how-to-find-tag-names/11802/1 "2016-06-11T17:37:38Z")

</div>

Hi,

In my project, tag history is stored in database, using historian concept. However, tags are stored under id and value columns.

I have to export a CSV file of tag history, for the previous month. I’m thinking to create a report with sql query, to fetch the tag values. However, tags are not stored in database using its names.

Please let me know how to find the tag names in database.

---

<div class="post-metadata">

**Author:** ![the\_msp](https://avatars.discourse-cdn.com/v4/letter/t/b38774/32.png) [@the\_msp](https://forum.inductiveautomation.com/u/the_msp)\
**Post date:** [June 13, 2016, 8:39am UTC](https://forum.inductiveautomation.com/t/tag-history-in-database-how-to-find-tag-names/11802/2 "2016-06-13T08:39:17Z")

</div>

Check out [docs.inductiveautomation.com/di … TagHistory](https://docs.inductiveautomation.com/display/DOC/system.tag.queryTagHistory) and below I have provided an example of my use of this.

I am reading from 64 historical tags and based on the timestamp then writing them to a memory tag in a folder with suffix ‘hist’.

The DB connection (in my gateway) is called ‘MySQL’ and you need to specify this in front of the tag path

dataSet = system.tag.queryTagHistory([color=#0040BF]paths=[’[MySQL]’ + path\_read], [/color]

[code]import datetime  
#startTime = event.source.parent.getComponent(‘Historical Replay’).timeCurrent  
#endTime = event.source.parent.getComponent(‘Date Range’).endDate

#use for testing in script console, comment out in designer  
startTime = datetime.datetime.now() - datetime.timedelta(days=10)  
endTime = startTime + datetime.timedelta(minutes=1)

nests = range(1,9)  
stations = [“1a”, “1b”, “2a”, “2b”]  
function = [“laser weld”, “leak test”]

#build path

for function in function:  
#system.tag.write(‘LW6APLC/script\_done’, 0)  
if function == “leak test”:  
a = "leak "  
elif function == “laser weld”:  
a = “”  
for station in stations:  
for nest in nests:  
path\_read = “LW6APLC/” + "positional " + function + " " + “fails/” + a + station + " " + “nest” + " " + str(nest)  
print path\_read  
dataSet = system.tag.queryTagHistory([color=#0040BF]paths=[’[MySQL]’ + path\_read], [/color]startDate=startTime, endDate=endTime, aggregationMode=“Maximum”, returnSize=0, returnFormat=‘Wide’)  
print dataSet  
y = system.dataset.toPyDataSet(dataSet)  
if len(y) \> 0:  
z = y[0][1]  
print z  
path\_write = “LW6APLC/” + "positional " + function + " " + “fails hist/” + a + station + " " + “nest” + " " + str(nest)  
print path\_write  
system.tag.write(path\_write, z)  
#system.tag.write(‘LW6APLC/script\_done’, 1)[/code]

---

<div class="post-metadata">

**Author:** ![sudhamoorthy](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sudhamoorthy](https://forum.inductiveautomation.com/u/sudhamoorthy)\
**Post date:** [June 20, 2016, 10:38am UTC](https://forum.inductiveautomation.com/t/tag-history-in-database-how-to-find-tag-names/11802/3 "2016-06-20T10:38:01Z")

</div>

Thanks a lot for your reply. It was very useful.

Regards  
Sudha

---

<div class="post-metadata">

**Author:** ![sudhamoorthy](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@sudhamoorthy](https://forum.inductiveautomation.com/u/sudhamoorthy)\
**Post date:** [June 23, 2016, 12:21pm UTC](https://forum.inductiveautomation.com/t/tag-history-in-database-how-to-find-tag-names/11802/4 "2016-06-23T12:21:00Z")

</div>

Hi,

I have a query here.

Is it possible to get the distinct tag values, using this ‘Tag History’ option? Will ‘Aggregation Mode’ be helpful in this case?

---

<div class="post-metadata">

**Author:** ![mramos](https://avatars.discourse-cdn.com/v4/letter/m/b3f665/32.png) [@mramos](https://forum.inductiveautomation.com/u/mramos)\
**Post date:** [October 16, 2024, 4:53pm UTC](https://forum.inductiveautomation.com/t/tag-history-in-database-how-to-find-tag-names/11802/5 "2024-10-16T16:53:25Z")

</div>

> [@Tagid listed under tag properties](https://forum.inductiveautomation.com/t/tagid-listed-under-tag-properties/8895):
>
> It would be a nice feature to have the tagid for the tag listed under the expanded properties under the sql tags browser. It’s kind of a pain to have to go to the sqlth\_te table to correlate the tag name with the tagid. I also found out today that there can be several retired tag ids for one tag under this folder, it led to some confusion to say the least. neutral_face Is this something that has been requested in the past? It seems like a simple yet useful addition. smiley -Dylan

> "have to go to the sqlth\_te table to correlate the tag name with the tagid"

---

<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:** [October 16, 2024, 6:13pm UTC](https://forum.inductiveautomation.com/t/tag-history-in-database-how-to-find-tag-names/11802/6 "2024-10-16T18:13:44Z")

</div>

The tag id changes whenever you reconfigure the tag, so no, this won't work.

Use the tag historian scripting functions to retrieve history by tag name/path. Don't attempt to access the SQL tables yourself (complicated and subject to change).

(Also, please don't revive such old topics.)
