SQLite Forum

ORDER BY not working for a specific DB/table
Login
Correct.  The order by and conditions should be rounded to the precision you want to see.  Example:

```
  select westccod, round(lati10, 6) as lati10, round(longi, 6) as longi
    from latiSaltxi 
   where round(lati10, 6) between 41.0 and 41.3 
order by round(lati10, 6), round(longi, 6)
;
```
or, more simply
```
with x(westccod, lati10, longi)
  as (
      select westccod, round(lati10, 6), round(longi, 6)
        from latiSaltxi
     )
  select *
    from x
   where lati10 between 41.0 and 41.3
order by lati10, longi
;
```
or, without the CTE
```
  select *
    from (
          select westccod, round(lati10, 6) as lati10, round(longgi,6) as longi
            from latiSaltxi
         )
   where lati10 between 41.0 and 41.3
order by lati10, longi
;
```

You could also make sure that your entire database is "consistently rounded" by updating it to consistent rounding before running your original query:

```
update latiSaltxi
   set lati10 = round(lati10, 6),
       longi = round(longi, 6)
;
```
before using your original query, choosing the rounding precision as appropriate for you to ensure that all the numbers are "similar representations".

An IEEE floating point number is only an approximation with certain guarantees as to the precision and accuracy of the approximation.  How well something complies with these guarantees (as in the answer contains slop, rather than being exactly rounded as required by the standard) varies considerably from compiler to compiler, runtime to runtime and Operating System to Operating System.  

Strictly speaking the purpose of IEEE floating point is to eliminate variable slop, and that the results obtained on one IEEE compliant system will exactly match the results obtained on another IEEE platform.  How a platform/runtime/compiler/operating system chooses to implement its interpretation of the IEEE standard varies however, leading to varying results (not to mention that the standard itself has bits "open to interpretation" and so different implementers have varying interpretations).

These are issues quite outside of SQLite3 since SQLite3 merely depends on the underlying platform implementation to "do the right thing".