LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

get maximum value in a field using SQL query

I'm a little late to the party, but want to add this for posterity.  You can use the DB Tools Select vi to get an aggregate from a table by putting the aggregation in the table name.

 

select MAX.png

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 11 of 19
(2,286 Views)

@jcarmody wrote:

I'm a little late to the party, but want to add this for posterity.  You can use the DB Tools Select vi to get an aggregate from a table by putting the aggregation in the table name.

 

select MAX.png


That is not a very good practice if you ask me. If you go far enough under the hood, you will find that you can even put conditions (WHERE ..., ORDER BY ...) after the table name too and it will work. But just because you can doesn't mean you should. 

 

And for the record, the easiest way to accomplish this IMO? Add "ORDER BY [column name] DESC" as the condition and take the first result. Boom, done.

Redhawk
Test Engineer at Moog Inc.

Saying "Thanks that fixed it" or "Thanks that answers my question" and not giving a Kudo or Marked Solution, is like telling your waiter they did a great job and not leaving a tip. Please, tip your waiters.

0 Kudos
Message 12 of 19
(2,275 Views)

One could also execute SQL like this:

 

SELECT * FROM MyTable WHERE MyField=(SELECT Max(MyField) From MyTable);

 

 

Message 13 of 19
(2,268 Views)

@drjdpowell wrote:

One could also execute SQL like this:

 

SELECT * FROM MyTable WHERE MyField=(SELECT Max(MyField) From MyTable);

 

 


Wouldn't 'SELECT Max(MyField) From MyTable' yield the same result?

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 14 of 19
(2,251 Views)

@jcarmody wrote:

I'm a little late to the party, but want to add this for posterity.  You can use the DB Tools Select vi to get an aggregate from a table by putting the aggregation in the table name.

 

select MAX.png


This may give you max(id), but not solve the problem. The problem is getting the whole row identified by max(id).

0 Kudos
Message 15 of 19
(2,245 Views)

@Yamaeda wrote:

@drjdpowell wrote:

One could also execute SQL like this:

 

SELECT * FROM MyTable WHERE MyField=(SELECT Max(MyField) From MyTable);

 

 


Wouldn't 'SELECT Max(MyField) From MyTable' yield the same result?


No.

 

Your solution only returns max(MyField).

The question was for the complete row for max(MyField), which is what the query from @drjdpowell will return.

Message 16 of 19
(2,240 Views)

@Yamaeda wrote:

@drjdpowell wrote:

One could also execute SQL like this:

 

SELECT * FROM MyTable WHERE MyField=(SELECT Max(MyField) From MyTable);

 

 


Wouldn't 'SELECT Max(MyField) From MyTable' yield the same result?


I think you mean 'SELECT *, Max(MyField) From MyTable', which gets all the fields of the maximum row.  

 

It depends if there is more than one row with the maximum value, in which case your version will only get one row, but the first version will get all such rows.  

Message 17 of 19
(2,237 Views)

@drjdpowell wrote:

@Yamaeda wrote:

 


Wouldn't 'SELECT Max(MyField) From MyTable' yield the same result?


I think you mean 'SELECT *, Max(MyField) From MyTable', which gets all the fields of the maximum row.  

 

It depends if there is more than one row with the maximum value, in which case your version will only get one row, but the first version will get all such rows.  


'SELECT *, Max(MyField) From MyTable' is quite non-standard SQL. Which database is this?

0 Kudos
Message 18 of 19
(2,218 Views)

That's SQLite: https://www.sqlite.org/lang_select.html

 

It may not apply to other DBs.

Message 19 of 19
(2,211 Views)