crazyvi
crazyvi

Reputation: 57

similar to last() in sql, what can i use in derby database?

Select Last(column_name) from table_name will return the last value in the column in standard SQL. What is a similar query that will work in derby to fetch the last value in the column?

Please find the sample table below: 'secondcol' is the column name in the table.

          secondcol
          33
          45
          78

Select Last(secondcol) from table_name in sql will return 78 as output. If i want to get similar output in javadb/derby database, how can i query it? I don't want to change any order in the column values.

Upvotes: 2

Views: 2182

Answers (2)

bwperrin
bwperrin

Reputation: 692

Is there some unique key on the table where you could form the question as "give me the secondcol value on the row with the max key value"? If so, there is a technique that will work in any database engine - the idea is to concatenate the key plus any desired result data, perform a min/max, then extract the result data.
See here and here.

Upvotes: 1

Yurdaaaaa
Yurdaaaaa

Reputation: 89

To select last row in table is little bit different then it is in pure SQL:

SQL:

SELECT * FROM tableName ORDER BY id DESC LIMIT 1;

DERBY:

SELECT * FROM tablename ORDER BY id DESC FETCH FIRST ROW ONLY;

Have a nice day ;)

Upvotes: 4

Related Questions