Try this if you want to display one of the repeating rows based on RequestID and CreatedDate and show the last HistoryStatus.
with t as (select row_number()over(partition by RequestID,CreatedDate order by RequestID) as rnum,* from tbltmp) Select RequestID,CreatedDate,HistoryStatus from ta 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 the repeating rows, taking into account only CreateDate and show the last HistoryStatus, 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 the repeating lines, counting only the request identifier, and show the last HistoryStatus, 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 ta where rnum in (SELECT Max(rnum) FROM t GROUP BY RequestID,CreatedDate having t.RequestID=a.RequestID)
All the above queries written on sql server 2005.
Himadri
source share