SQLite User Forum

select max not working
Login

select max not working

(1) By anonymous on 2022-04-24 14:24:02 [link] [source]

Hello.  I would like some help here on the max sqlite command.  

I have the following dataset from a database.

sqlite> select distance from qsos;
1284
1734
1382
1489
2713
935
0
990
1408
2598
1136
1136
1243
1813
1263
1300
1220
2058
2726
1313


When I issue the command select max(distance) from qsos, it doesn't return the largest number?  


sqlite> select max(distance) from qsos;
990

I have the syntax wrong, but don't know what the correct syntax should be.  SQL nube here....

Thanks
Kyle

(2) By Stephan Beal (stephan) on 2022-04-24 14:58:00 in reply to 1 [link] [source]

I have the syntax wrong, but don't know what the correct syntax should be.

The syntax is correct. The problem you're see is almost always caused by the data being stored as strings instead of numbers, and strings compare differently than numbers. The quick and dirty workaround is something like:

> select max(cast(distance as integer)) from qsos;

The proper solution is to re-save the data as numbers instead of strings.

(3) By anonymous on 2022-04-24 15:00:25 in reply to 2 [source]

Yep, your totally correct. Thank you!!!! Kyle