# Daily Earliest Tag Value Extraction

**URL:** <https://forum.inductiveautomation.com/t/daily-earliest-tag-value-extraction/34883>\
**Category:** General Discussion\
**Created:** [May 19, 2020, 3:28pm UTC](https://forum.inductiveautomation.com/t/daily-earliest-tag-value-extraction/34883 "2020-05-19T15:28:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasilevdim](https://avatars.discourse-cdn.com/v4/letter/v/7ba0ec/32.png) [@vasilevdim](https://forum.inductiveautomation.com/u/vasilevdim)\
**Post date:** [May 19, 2020, 3:28pm UTC](https://forum.inductiveautomation.com/t/daily-earliest-tag-value-extraction/34883/1 "2020-05-19T15:28:32Z")

</div>

Hello all,

I am trying to extract a counter value once the machine is ON, at the start of the first shift.  
I used query tag (cast function and MIN value involved), but in some cases, it does not work.  
So, I was wondering, what different approaches can be applied?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [May 19, 2020, 8:41pm UTC](https://forum.inductiveautomation.com/t/daily-earliest-tag-value-extraction/34883/2 "2020-05-19T20:41:46Z")

</div>

You could use a value change or quality change event on your ‘Machine ON’ tag. Something like

```auto
if currentValue.value == 'ON': //only works when value was OFF and is now ON
  //Read the odometer count tag
  count = system.tag.read('counterTagPath').value  
  //Write count to memory tag for holding start count
  system.tag.write('dailyStartCountTag',count)
```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 20, 2020, 11:18am UTC](https://forum.inductiveautomation.com/t/daily-earliest-tag-value-extraction/34883/3 "2020-05-20T11:18:26Z")

</div>

What about querying to the min timestamp of your data?

---

<div class="post-metadata">

**Author:** ![vasilevdim](https://avatars.discourse-cdn.com/v4/letter/v/7ba0ec/32.png) [@vasilevdim](https://forum.inductiveautomation.com/u/vasilevdim)\
**Post date:** [May 20, 2020, 5:27pm UTC](https://forum.inductiveautomation.com/t/daily-earliest-tag-value-extraction/34883/4 "2020-05-20T17:27:44Z")

</div>

Hello, JordanCClark, how such query will looks like?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 21, 2020, 10:40am UTC](https://forum.inductiveautomation.com/t/daily-earliest-tag-value-extraction/34883/5 "2020-05-21T10:40:22Z")

</div>

That depends, what does your table look like? 😉  
Also, what database are you using?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 21, 2020, 10:44am UTC](https://forum.inductiveautomation.com/t/daily-earliest-tag-value-extraction/34883/6 "2020-05-21T10:44:58Z")

</div>

An example may look like:

```sql
SELECT count_value 
FROM my_table
WHERE t_stamp > '2020-05-21 06:00:00'
ORDER BY t_stamp
LIMIT 1

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 21, 2020, 10:46am UTC](https://forum.inductiveautomation.com/t/daily-earliest-tag-value-extraction/34883/7 "2020-05-21T10:46:04Z")

</div>

You could also use less than, if you want the last value before the start of shift.

```sql
SELECT count_value 
FROM my_table
WHERE t_stamp < '2020-05-21 06:00:00'
ORDER BY t_stamp DESC
LIMIT 1
```

---

<div class="post-metadata">

**Author:** ![vasilevdim](https://avatars.discourse-cdn.com/v4/letter/v/7ba0ec/32.png) [@vasilevdim](https://forum.inductiveautomation.com/u/vasilevdim)\
**Post date:** [May 22, 2020, 2:04pm UTC](https://forum.inductiveautomation.com/t/daily-earliest-tag-value-extraction/34883/8 "2020-05-22T14:04:07Z")

</div>

Got.error "incorrect syntax near LIMIT…"Strange, because the syntax seems correct.

---

<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:** [May 22, 2020, 4:36pm UTC](https://forum.inductiveautomation.com/t/daily-earliest-tag-value-extraction/34883/9 "2020-05-22T16:36:56Z")

</div>

What kind of database? Some don’t have `LIMIT`, but use `TOP n` instead.

---

<div class="post-metadata">

**Author:** ![vasilevdim](https://avatars.discourse-cdn.com/v4/letter/v/7ba0ec/32.png) [@vasilevdim](https://forum.inductiveautomation.com/u/vasilevdim)\
**Post date:** [May 22, 2020, 5:08pm UTC](https://forum.inductiveautomation.com/t/daily-earliest-tag-value-extraction/34883/10 "2020-05-22T17:08:32Z")

</div>

Oh, yes, it is SQL Express.

---

<div class="post-metadata">

**Author:** ![vasilevdim](https://avatars.discourse-cdn.com/v4/letter/v/7ba0ec/32.png) [@vasilevdim](https://forum.inductiveautomation.com/u/vasilevdim)\
**Post date:** [May 24, 2020, 10:44am UTC](https://forum.inductiveautomation.com/t/daily-earliest-tag-value-extraction/34883/11 "2020-05-24T10:44:25Z")

</div>

The proper syntax for SQL Server Express (my case) is:  
SELECT TOP 1 count\_value  
FROM my\_table  
WHERE t\_stamp \< ‘2020-05-21 06:00:00’  
ORDER BY t\_stamp DESC,

but such query will not work for me, since it uses fixed date, and I need this query to be executed every time the machine has been started, on the current date.
