Click here to Skip to main content
15,879,074 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more: , +
Here is the Scenario
I have table like this
OppID ModifyDate Old_Stage Value New_Stage Value

Opp_1 01-01-2013 Null Hold
Opp_1 05-11-2014 Hold Lost


I want to extract the latest Modify Date wrt Opportunity.
Note: I Need to Join this table with Others.
How can i write the query. Please Help !!
Posted

SQL
select * from Table order by ModifyDate desc 


Will give you data by descending order of modification.

you can choose to have

SQL
select top 1 * from Table order by ModifyDate desc  


to get latest modification date.

Other way is to apply Ranking function

SQL
select *,Row_Number() Over (order by ModifyDatedesc ) As LatestRecord from Table order by ModifyDate desc


and then you can filter LatestRecord column on 1 which will hold latest date.

You can also check this

SQL
SELECT * FROM TABLENAME ORDER BY PRIMARYCOLUMNNAME DESC
FETCH FIRST 1 ROWS ONLY ;


BUT THIS OPTION IS VALID ONLY IF THERE IS A PRIMARY KEY
COLUMN,OTHERWISE IT WONT WORK
 
Share this answer
 
v5
Comments
lovejeet0707 20-Nov-14 6:26am    
I have Hundred Thousands Record like this in my Table.Its not feasible for me to first sort the data and than select top 1.
So I dont think first approach works.

and I am still doubtful what the second query is doing....!
Jörgen Andersson 20-Nov-14 7:14am    
To get the latest ModifyDate you NEED to sort the data. If you find that this is not feasible it's only because you lack a proper index.
Add an index on (OppID,ModifyDate) and query with select TOP 1 * from Table where OppID = @OppID order by ModifyDate desc
Herman<T>.Instance 24-Nov-14 9:21am    
I you create a descending Index om ModifyDate then top 1 is he first hit and always keeps on speed.
Shweta N Mishra 20-Nov-14 7:02am    
select * from
(
select *,Row_Number() Over (order by ModifyDatedesc ) As LatestRecord from Table order by ModifyDate desc
) a where LatestRecord=1

You will get the latest record.
Jörgen Andersson 20-Nov-14 7:14am    
Compensating faulty downvote
Your question is a bit short on information.
But I believe something like this may work:
SQL
select 
    opp.*,
    oppi.*
from Opportunity opp
inner join (
          select OppID, max(ModifyDate) ModifyDateMax
          from Opportunity
          group by OppID
    ) oppMaxDt
    on 
          opp.OppID = oppMaxDt.OppID
          and 
          opp.ModifyDate = oppMaxDt.ModifyDateMax 
--additional table join(s) can go here
inner join Opp_Info oppi
	on opp.OppID = oppi.[Opp ID]
;

note: OppID and ModifyDate need to be unique for the query to return one record for each OppID and max ModifyDate
 
Share this answer
 
v3
Comments
lovejeet0707 20-Nov-14 6:34am    
HiActually problem is this only, I have Same Opportunity ID and different rows.
I just need the most recently Updated row.
jaket-cp 20-Nov-14 6:43am    
Not sure what you are saying.
Are you saying the value for OppID never changes?
lovejeet0707 20-Nov-14 6:44am    
Actually Query is Working out for me.....!
Thanks
lovejeet0707 20-Nov-14 6:51am    
How can I write the additional join table,
for example Table Account - AccountName,AccountID
Contact- ContactName, ContactID.
Thanks in Advance :)
jaket-cp 20-Nov-14 6:57am    
If the opportunity table has a AccountID or ContactID, then you can join it with that.
Have a read of http://www.w3schools.com/sql/sql_join.asp
SQL
Select OppID, ModifyDate ,Old_Stage_Value, New_Stage_Value from tablename
where ModifyDate in(Select max(ModifyDate) from tablename group by OppID)


it will display latest OppID data
check it will help
 
Share this answer
 
v2
Hi
friend You also try this solution


SQL
DECLARE @Temp as table(fbalance int,fdate nvarchar(50),facno nvarchar(50))
insert into @Temp VALUES(100000.00,'Opp_2 01-01-2013','010001801000009')
insert into @Temp VALUES(80000.00,'Opp_1 05-11-2014','010001801000010')

SELECT * FROM @Temp
ORDER by SUBSTRING(fdate,len(fdate)-9,len(fdate)-(len(fdate)-10)) DESC
 
Share this answer
 

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