# SQL Query tag, need to pull information from one of two rows

**URL:** <https://forum.inductiveautomation.com/t/sql-query-tag-need-to-pull-information-from-one-of-two-rows/72300>\
**Category:** Ignition\
**Tags:** scripting, database, ignition79\
**Created:** [April 11, 2023, 1:31pm UTC](https://forum.inductiveautomation.com/t/sql-query-tag-need-to-pull-information-from-one-of-two-rows/72300 "2023-04-11T13:31:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Eubanks](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Michael\_Eubanks](https://forum.inductiveautomation.com/u/Michael_Eubanks)\
**Post date:** [April 11, 2023, 1:31pm UTC](https://forum.inductiveautomation.com/t/sql-query-tag-need-to-pull-information-from-one-of-two-rows/72300/1 "2023-04-11T13:31:30Z")

</div>

I've got a series of SQL Queries that need to pull information from a database that 2 different devices report to. They both create a new row at around the same time, and are not always consistent in which comes first. So the information I need could be in either the last or second to last row of the table.

I've got a script that pulls in the information in the databse query browser, but when I try and use it in a SQL Query tag it fails to work.

SELECT testnum FROM welltest\_welltest  
WHERE  
testnum \>= (SELECT MAX(testnum)-1 FROM welltest\_welltest)  
AND module\_id = 'cc663ec0-28dc-4e06-8549-d3df132a1ddd' --SP 330

In this case the result should be an integer value, but I need to be able to repeat this for several other values like string, float, and datetime.

---

<div class="post-metadata">

**Author:** ![justin.brzozoski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justin.brzozoski/32/13452_2.png) [@justin.brzozoski](https://forum.inductiveautomation.com/u/justin.brzozoski)\
**Post date:** [April 11, 2023, 1:40pm UTC](https://forum.inductiveautomation.com/t/sql-query-tag-need-to-pull-information-from-one-of-two-rows/72300/2 "2023-04-11T13:40:22Z")

</div>

The preferred way to find rows with the min/max value in a column is to use ORDER and LIMIT like this:

```plaintext
SELECT testnum
FROM welltest_welltest
WHERE module_id = 'cc663ec0-28dc-4e06-8549-d3df132a1ddd'
ORDER BY testnum DESC LIMIT 2;

```

---

<div class="post-metadata">

**Author:** ![Michael\_Eubanks](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Michael\_Eubanks](https://forum.inductiveautomation.com/u/Michael_Eubanks)\
**Post date:** [April 11, 2023, 3:40pm UTC](https://forum.inductiveautomation.com/t/sql-query-tag-need-to-pull-information-from-one-of-two-rows/72300/3 "2023-04-11T15:40:53Z")

</div>

Thanks, I'll keep that in mind next time I need to order things. The issue was actually that the SQL query tag's query type was set to automatic, while I needed to specify it as a select function. I think the fact that I had the second select function in the original code with the max function is what was throwing it off.

---

<div class="post-metadata">

**Author:** ![justin.brzozoski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justin.brzozoski/32/13452_2.png) [@justin.brzozoski](https://forum.inductiveautomation.com/u/justin.brzozoski)\
**Post date:** [April 11, 2023, 4:35pm UTC](https://forum.inductiveautomation.com/t/sql-query-tag-need-to-pull-information-from-one-of-two-rows/72300/4 "2023-04-11T16:35:37Z")

</div>

Actually, it occurred to me that my answer is MySQL/MariaDB specific. If you're using other database server types it might be different. (I'm just used to thinking in terms of those DBs all the time and forget others exist...)
