Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any thing such as SELECT LAST in sql query?

Tags:

sql

sybase

I am using sybase database to query the daily transaction report. I had subquery within my script.

Here as it goes:

SELECT orders.accountid ,items.x,etc
(SELECT charges.mistotal FROM charges where items.id = charges.id)
FROM items,orders
WHERE date = '2008-10-02'

Here I am getting the error message as:

Subquery cannot return more than one values

My values are 7.50, 25.00

I want to return the 25.00, but when I use

(SELECT TOP 1 charges.mistotal FROM charges where items.id = charges.id)

My result is 7.50 but I want to return 25.00

Does anyone has any better suggestion?

like image 328
jbcedge Avatar asked Oct 02 '08 00:10

jbcedge


People also ask

How do I select last data in SQL?

We can use the ORDER BY statement and LIMT clause to extract the last data. The basic idea is to sort the sort the table in descending order and then we will limit the number of rows to 1. In this way, we will get the output as the last row of the table. And then we can select the entry which we want to retrieve.

Is there a select bottom in SQL?

No, there is no BOTTOM operator.

Is there a last function in SQL?

The LAST() function returns the last value of the selected column.

What is the last clause in SQL query?

SQL LAST() function returns the last value of the given column. SQL LEN() function returns the total length of the given column.


1 Answers

SELECT TOP 1 * 
FROM dbo.YourTable 
ORDER BY Col DESC

In your case, I guess that would be

SELECT TOP 1 charges.mistotal 
FROM charges where items.id = charges.id 
ORDER BY charges.mistotal DESC
like image 83
senfo Avatar answered Oct 06 '22 00:10

senfo