I am using Sqlite 3.
I need to know whether the current row is the last one in a SQL query result, before invoking the sqlite3_step().
After checking the existing post
SQLite3 - how to know if the current row is the last row
and
How to check if the current row is the last selected row in a sql query?
Since each row in SQlite3 has a ROWID, I figure out a way to write the code, as follows:
SELECT (ROWID = MAX(ROWID)) AS IsLast FROM MyTable ORDER BY ROWID;
The data in MyTable is:
|BID|
|3|
|2|
|5|
|7|
The expected result is all rows except the last one is FALSE, as below:
|0|
|0|
|0|
|1|
But the actual result is:
|1|
Moreover, I try to integrate the code into a more complex query, which comes from SQL select only rows with max value on a column as below:
SELECT a.id, a.rev, a.contents, b.mycount, (a.ROWID = MAX(a.ROWID)) AS IsLast
FROM YourTable a
INNER JOIN (
SELECT id, MAX(rev) rev, COUNT(id) mycount
FROM YourTable
GROUP BY id
) b ON a.id = b.id AND a.rev = b.rev ORDER BY a.ROWID;
Take the original table in the original post as an example:
+------+-------+--------------------------------------+
| id | rev | content |
+------+-------+--------------------------------------+
| 1 | 1 | ... |
| 2 | 1 | ... |
| 1 | 2 | ... |
| 1 | 3 | ... |
+------+-------+--------------------------------------+
After using the query, the expected result should be(the last column is IsLast flag):
|2|1|...|1|0|
|1|3|...|3|1|
But the actual result is only one row:
|1|3|...|3|1|
What is the problem?
Thanks