The query: select distinct projid from task order by changed desc limit 5
If I run this, I receive five rows, as expected. However, if one uses a database tool (e.g. Valentina Studio or SqliteManager) and asks it to sort on the changed column (descending), rows appear that should be included in the result of the query but are not (the query should include only one row because of “distinct”).
There is another column in the table (“taskid”), and it is blank for the rows in question. If I change the query to “select distinct projid, taskid…”, I get the same result. If I put a value in the taskid column (anything), I get the expected result.
I’ve created a database that shows this behavior (ripped out all non-relevant tables and columns), however, it’s not obvious how to upload it.
I’m not sure what is the problem that you are seeing. Using ‘limit 5’ at the end only shows 5 records, removing that shows 17 using Valentina. What do you get?
EDIT: as far as I can see all ‘projid’ are distinct
If from those you want projects in order of last changes, reorder the findings:
SELECT
*
FROM
( SELECT projid, max( changed ) last_change FROM task GROUP BY projid ORDER BY projid ASC, changed DESC ) recent_ones
ORDER BY
last_change DESC
If you want the top 5:
SELECT
*
FROM
( SELECT projid, max( changed ) last_change FROM task GROUP BY projid ORDER BY projid ASC, changed DESC ) recent_ones
ORDER BY
last_change DESC
LIMIT 5;
Ok, query: SELECT DISTINCT projid FROM task ORDER BY changed DESC LIMIT 5
you have several projid = JAJJDSYXAIZ right? each one with different changed so what you expect is that SQLite assign the max changed value to JAJJDSYXAIZ so that will show on your list but that is not how SQLite works for this, you will need to use a different query.
This query from Rick is what you want (I think):
SELECT
*
FROM
( SELECT projid, max( changed ) last_change FROM task GROUP BY projid ORDER BY projid ASC, changed DESC ) recent_ones
ORDER BY
last_change DESC
LIMIT 5;
or
SELECT
projid
FROM
( SELECT projid, max( changed ) last_change FROM task GROUP BY projid ORDER BY projid ASC, changed DESC ) recent_ones
ORDER BY
last_change DESC
LIMIT 5;