i see that @Villeroy has already responded & provided answers/suggestions based on the uploaded db.
my response is more general & based on the initial post made by @charlie.it.
essentially we have two queries an outer query & a correlated sub-query.
a correlated sub-query is a query nested within another query that uses values from that other query.
outer query:
correlated sub-query:
Code: Select all
( SELECT COUNT( * ) + 1 FROM "test" "t2" WHERE "t2"."ID" < "test"."ID" ) "ID1"
both outer & inner queries select data from the very same single table "test”.
the outer query is self explanatory.
the sub-query simply counts the number rows in the table "test" where value of "ID" is less than the currently selected value of "ID" in the outer query.
because both outer & inner queries reference the same table it's necessary to assign an alias "t2" to the table of the inner query in order to distinguish them.
Your query could be coded as below.
Note: the 'AS' keyword is optional.
Code: Select all
SELECT "ID", "name", ( SELECT COUNT( * ) FROM "test" AS "t2" WHERE "t2"."ID" <= "test"."ID" ) "ID1" FROM "test"
if "ID" = 1, 5, 9 then the sub-query will return 1, 2, 3.
if "ID" = 1, 5, 5, 9 then the sub-query will return 1, 3, 3, 4.
so provided the value of "ID" is unique the sub-query always returns a sequential result equivalent to table row number ordered by "ID" & is very often used to self join a table when using the default embedded hsql 1.8.0.10 which lacks the rownum() function.
open office 4.1.14 & LibreOffice 6.4.4.2 x64 using HSQL 1.8.0.10 (Embedded) and HSQL 2.6.0 (Split) on Windows 10