# [ACCEPTED]-Is there any thing such as SELECT LAST in sql query?-sybase

Score: 11
``````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
``````

0

Score: 2

Under what criteria you choose to select 7 the 25.00 instead of the 7.5?

If its related 6 to the maximum value, you can try using 5 the MAX() function on that field.

If its related 4 to the chronologically last row added, try 3 using the MAX() on the datetime field, if you 2 have details on the hours and minutes it 1 was added.

Score: 1

You could try this:

``````SELECT MAX(charges.mistotal) FROM charges WHERE items.id = charges.id
``````

0

Score: 1

So, can you use inverse order:

``````(SELECT TOP 1 charges.mistotal
FROM charges
WHERE items.id = charges.id
ORDER BY charges.mistotal DESC
)
``````

Actually, since 8 you didn't give an explicit order, the sequence 7 of the returned results is undefined, and 6 you are just lucky that it gave you the 5 answer you didn't want; it could have given 4 you the answer you wanted, and then you 3 might not have noticed that it was not always 2 correct until after it went into production.

Or, can 1 you use:

``````(SELECT MAX(charges.mistotal)
FROM charges
WHERE charges.id = items.id
)
``````

Or did you really want a SUM?

Score: 1

To get first you use select top 1 | first 2 * from table order ascending to get last, just invert 1 your order.

Score: 0

SELECT TOP 1 charges.mistotal FROM charges 6 where items.id = charges.id ORDER BY charges.id 5 DESC

The order by clause will make sure it 4 comes back in the order of the id, and the 3 DESC means descending so it will give you 2 the largest (newest) value first. TOP 1 1 of course makes sure you just get that one.

Score: 0

Sort your subquery. If you want the "last" value, you 13 need to define how you determine which item 12 comes last (remember, SQL result sets are 11 unordered by default).

For example:

``````(SELECT TOP 1 charges.mistotal FROM charges where items.id = charges.id
ORDER BY charges.mistotal DESC)
``````

This would 10 return 25.00 instead of 7.50 (from your 9 data examples above), but I'm assuming that 8 you want this value to be "last" because 7 it's bigger. There may be some other field 6 that it makes more sense for you to sort 5 on; maybe you have a timestamp column, for 4 example, and you could sort on that to get 3 the most recent value instead of the largest 2 value. The key is just defining what you 1 mean by "last".

More Related questions