tags:

views:

235

answers:

1

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);
+1  A: 

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
hmmm..not happening =(
Tommy
Does it work when you execute it directly, using `SQLite Manager` for example?
Peter Lang
Interesting - using the manager with both of your examples gives me the same result as the basic "Select thecolumn From your_table". Strange, something must be messed up.
Tommy
And it works without the `CAST` (except that it is not sorted by numbers)?
Peter Lang
yes..............
Tommy
I added another way - casting in the sub-query. Good luck :)
Peter Lang