I want to fetch last value in a column in a row in a derby db table. Can someone help me?
Asked
Active
Viewed 1,962 times
0
-
2"Last" depending on what? Rows in a relational database are ***not*** sorted. So you will have to use some kind of `order by` criteria. Please supply us with some sample data and expected output. – Feb 28 '13 at 09:03
-
Say for example, What you can see below is the data in the table in derby db. firstcol secondcol Thirdcol 1 33 33 2 45 55 3 78 79 I want to fetch last value in the ‘secondcol’ column. The output should be 78. For this desired output, how can I query it from derby database table. – crazyvi Feb 28 '13 at 09:30
-
Please edit your question and format the data properly. Why is 78 the "last" value, if there is a *79*? – Feb 28 '13 at 09:35
-
i will try to give u in a better format. – crazyvi Feb 28 '13 at 09:38
-
Sorry,I m not able to format the sample data table,whatever I do the format changes. So ill describe you. Imagine a table which has ‘secondcol’ as a column. The values in the column are 33,45,78. I want to fetch last value in the ‘secondcol’ column. The output should be 78. For this desired output, how can I query it from derby database table. – crazyvi Feb 28 '13 at 10:18
-
1select * from mytable where secondcol = (select max(secondcol) from mytable) – Bryan Pendleton Feb 28 '13 at 14:54
-
I want the most recently inserted value in the column irrespective of min or max in the column value. In sql, we can use last(column_name) from table_name, which would fetch the last inserted value in the column. If I wish to get the output in similar way in derby what can i use? – crazyvi Mar 01 '13 at 08:32
3 Answers
0
SELECT *
FROM dbo.rawImport
WHERE number= ( select max(number)
from dbo.rawImport)

Waller
- 1,795
- 4
- 18
- 35
-
Damn, let me edit it. Wrote it quickly yesterday, would something like this work? – Waller Mar 01 '13 at 09:54
0
Try this one if you are trying to get last id (numeric value)
SELECT MAX(COLUMN_NAME)
FROM TABLE_NAME
thats it.
And if you want to get whole data of last row then call getRowCount()
method and select the record at that reference. check out code example.
getRowCount() method:
public int getRowCount(String tableName) {
int count = 0;
try {
res = st.executeQuery("SELECT COUNT(*) FROM " + tableName);
while (res.next()) {
count = res.getInt(1);
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Table not found record getting");
}
return count;
}
It will return an int value so use it as
SELECT * FROM TABLE_NAME WHERE ID = getRowCount(TABLE_NAME);
And the result is your last record.

Inzimam Tariq IT
- 6,548
- 8
- 42
- 69