2

Including DISTINCT to an SQL query that also uses ORDER BY CAST(thecolumn AS int) as shown here seems to remove that sorting functionality.

Any reason these cant work together? (Using sqlite with the C api)

Thanks.

EDIT:
Started with -

sprintf(sql, "SELECT DISTINCT rowX FROM TableX Order By Cast(rowX As int) LIMIT 150 OFFSET %s;", Offset);

rowX is Type CHAR(5)

NOW:

sprintf(sql, "Select rowX FROM(Select Distinct rowX From TableX)t Order By Cast(rowX As int) LIMIT 150 OFFSET %s;", Offset);
Community
  • 1
  • 1
T.T.T.
  • 33,367
  • 47
  • 130
  • 168

2 Answers2

2

I used the following with sqlite, and sorting worked fine:

Select Distinct thecolumn
From your_table
Order By Cast(thecolumn As int)

Have you tried putting the DISTINCT into a sub-query?

Select thecolumn
From
(
  Select Distinct thecolumn
  From your_table
) t
Order By Cast(thecolumn As int)

I would expect it to work that way.


One more way:

Select thecolumn
From
(
  Select Distinct Cast(thecolumn As int) As thecolumn
  From your_table
) t
Order By thecolumn
Peter Lang
  • 54,264
  • 27
  • 148
  • 161
0

This is way super late, but the order by has to match the select list exactly so:

select distinct cast(column as int)
from table
order by cast(column as int)
sds
  • 58,617
  • 29
  • 161
  • 278
anon
  • 1