Wilson WindowWare Tech Support

WinBatch WinBatch+Compiler WebBatch
Home | Tech Database | Tech BBS | White Papers | Purchase


Retrieve All Columns From the Last Row of the table

 Keywords: Retrieve * Columns Last Row  

Question:

Hopefully a quick question for you. I'm simply trying to retrieve the entire last row in a table. I want to get all the columns, but something like: "Select columnA, columnb, max(stamp) from table" doesn't work, even using the group by function.

I'm plugging temps in a table every 5 minutes, I want to pull the latest reading and put it on a screen. All fields are 1/0 except the stamp field which is date.

Can you help?

Answer:

Maybe you want to include the following in your SQL Statment:
ORDER BY {Col} DESC
ORDER BY is an optional clause which will allow you to display the results of your query in a sorted order (either ascending order or descending order) based on the columns that you specify to order by.

DESC would cause the Last item to now become the first in the result set. So you might be able to do a single qfetch....

Here is a good SQL reference website:http://sqlcourse2.com/orderby.html

More....

Another user suggested the following SQL statment, might work also:
SELECT columnA, columnb, stamp FROM table 
WHERE 
stamp IN(SELECT MAX(stamp) FROM table)

Conclusion:

Thanks to all. I found the following works fine:
SELECT TOP 1 * FROM tbl_ping ORDER BY stamp DESC
Then I can simply read the first record and throw the rest out.
Article ID:   W15060