Try this if you want to display one of duplicate rows based on RequestID and CreatedDate and show the latest HistoryStatus.
with t as (select row_number()over(partition by RequestID,CreatedDate order by RequestID) as rnum,* from tbltmp)
Select RequestID,CreatedDate,HistoryStatus from t a where rnum in (SELECT Max(rnum) FROM t GROUP BY RequestID,CreatedDate having t.RequestID=a.RequestID)
or if you want to select one of duplicate rows considering CreatedDate only and show the latest HistoryStatus then try the query below.
with t as (select row_number()over(partition by CreatedDate order by RequestID) as rnum,* from tbltmp)
Select RequestID,CreatedDate,HistoryStatus from t where rnum = (SELECT Max(rnum) FROM t)
Or if you want to select one of duplicate rows considering Request ID only and show the latest HistoryStatus then use the query below
with t as (select row_number()over(partition by RequestID order by RequestID) as rnum,* from tbltmp)
Select RequestID,CreatedDate,HistoryStatus from t a where rnum in (SELECT Max(rnum) FROM t GROUP BY RequestID,CreatedDate having t.RequestID=a.RequestID)
All the above queries I have written in sql server 2005.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…