Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
2.71/5 (4 votes)
See more:
Hi everyone out there.Please I want to select the last row record and below is the code I have but am not getting the result I want any help will be grateful.

I appreciate all your effort in trying to help me solve this problem. I have try all the solutions you have provided and am still not getting the result I want. So let me use sample data below to explain things very well.

Example
LedgerNumber LedgerName Amount
4 Gold 300
7 Rice 280
7 Rice 500
4 Gold 80
4 Gold 120
7 Rice 550

Here the sample records above has six rows with three columns. Let say I want to select the last Amount with a LedgerName called Gold. The last amount in this sample data for ledgerName Gold is 120 but the last amount in the table is 550 which has a ledgername called Rice and that is not the target. I hope this explanation will give readers better understanding.

SQL
Select Top (1) Amount From Transactions
Where LedgerName = 'ABC'
Order By LedgerName DESC


Thanks.
Posted
Updated 8-Jul-14 13:50pm
v2
Comments
[no name] 8-Jul-14 7:45am    
What is the input? What is the output? What are you getting? How is it not what you want?
Sarvesh Kumar Gupta 8-Jul-14 8:08am    
Why you r using Order By, when you r fetching records only for ABC.
[no name] 8-Jul-14 21:02pm    
I don't think you can unless you have a column with a unique value to run your query against.

try this.. it works
SQL
Select Top (1) Amount From Transactions
Where LedgerName = 'ABC'
Order By Amount DESC
 
Share this answer
 
Try
Select Top (1) Amount From Transactions<br />
Where LedgerName = 'ABC'<br />
Order By Amount DESC


Replace LedgerName with Amount. Since you choose ABC as the LedgerName, it is the same anyway.
 
Share this answer
 
if you are really confuse with this solution. THen leave it just do it LINQ and it's simple
no need to order by any thing Just use Last or LastOrDefault method it will return you last record of the table
Get more info
http://www.dotnetperls.com/lastordefault[^]
play-with-last-and-lastordefault-in-linq-to-entities.aspx[^]
 
Share this answer
 
Logically your table should also have a primary key.
If you have it then the below query must work for you

SQL
select t1.primarykey, t1.LedgerNumber, t1.LedgerName , t1.Amount
from table1 t1
inner join
    (
    select LedgerNumber , max(primarykey) primarykey
    from table1
    group by LedgerNumber
    ) t2 on t2.primarykey = t1.primarykey
 
Share this answer
 
using this code

SQL
select * from Transactions
 where LedgerName in (select top 1 LedgerName from Transactions
Where LedgerName ='ABC'
)
Order By LedgerName DESC
 
Share this answer
 
v2
Comments
Sarvesh Kumar Gupta 8-Jul-14 8:08am    
Why you r using Order By, when you r fetching records only for ABC.
SQL
select top 1* from(
Select Row_number()over(order by Amount desc)as rowno,*from Transactions Where LedgerName = 'ABC'
)temp
 
Share this answer
 
Only need to put DESC and LedgerName before top (1)

SQL
Select Top (1) LedgerName,  Amount From Transactions
Where LedgerName = 'ABC'
Order By LedgerName DESC




Please clear, do you want to search amount only or can ok with tweo column in out put ?
 
Share this answer
 
v3
Comments
Sarvesh Kumar Gupta 8-Jul-14 8:07am    
Why you r using Order By, when you r fetching records only for ABC.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900