Hi, I have a query that would need to get the last touched or transaction after workid "FJ_CBDEF". Below is my initial query and DDL. Any idea is very much appreciated.The result for the 2 Item code should be "FJ_LMDADS" and "FJ_CBVFI2" Thank you in advance.Create table #maintable(Item nvarchar(35), Itemcode nvarchar(35))Insert into #maintable(item,itemcode) values ('AAAAA','RPT00000001')Insert into #maintable(item,itemcode) values ('BBBBB','RPT00000002')Create table #Table1(Itemcode nvarchar(35), RankNum int, WorkID nvarchar(35), CreatedDatetime datetime)Insert into #table1(itemcode, RankNum, WorkID,CreatedDatetime) values ('RPT00000001','5','FJ_Pentry','2015-01-12 23:50:48.000')Insert into #table1(itemcode, RankNum, WorkID,CreatedDatetime) values ('RPT00000001','15','FJ_CBDEF','2015-01-14 05:23:10.000')Insert into #table1(itemcode, RankNum, WorkID,CreatedDatetime) values ('RPT00000001','10','FJ_LMDADS','2015-01-13 15:03:56.000')Insert into #table1(itemcode, RankNum, WorkID,CreatedDatetime) values ('RPT00000002','0','FJ_Pentry','2015-01-12 23:53:47.000')Insert into #table1(itemcode, RankNum, WorkID,CreatedDatetime) values ('RPT00000002','10','FJ_LMDADS','2015-01-13 14:52:15.000')Insert into #table1(itemcode, RankNum, WorkID,CreatedDatetime) values ('RPT00000002','15','FJ_DiAss','2015-01-13 16:24:35.000')Insert into #table1(itemcode, RankNum, WorkID,CreatedDatetime) values ('RPT00000002','20','FJ_CBVFI2','2015-01-13 19:47:36.000')Insert into #table1(itemcode, RankNum, WorkID,CreatedDatetime) values ('RPT00000002','25','FJ_CBDEF','2015-01-14 05:22:52.000')Create table #Table2(Itemcode nvarchar(35), RankNum int, WorkID nvarchar(35), CreatedDatetime datetime, FCode nvarchar(5))Insert into #table2(itemcode, RankNum, WorkID, CreatedDatetime, FCode) values ('RPT00000001','10','FJ_LMDAD','2015-01-12 23:50:48.000','FJ001')Insert into #table2(itemcode, RankNum, WorkID, CreatedDatetime, FCode) values ('RPT00000002','20','FJ_CBVFI2','2015-01-13 19:47:36.000','FJ004')Select m.Item, m.Itemcode, t1.RankNum, t1.WorkID, t1.CreatedDatetime, t2.FCode,rn=ROW_NUMBER() OVER (partition by m.Itemcode order by m.Itemcode , t1.CreatedDatetime desc )From #maintable mLeft join #Table1 t1On t1.Itemcode = m.Itemcode Left Join #Table2 t2On t2.Itemcode = t1.Itemcode and t2.RankNum = t1.RankNum Order by m.Itemcode, t1.RankNum