/*1 Use a subquery to filter out ParentID NULL*/Select * fromBid_projectpackagealtereddesigninfo t0where exists( Select 1 from ( SelectParentID,Max(Altertime) asAltertime fromBid_projectpackagealtereddesigninfoGroup byparentid) xwhereX.parentid=T0. ParentID andX.altertime=t0. Altertime)SelectA.* fromBid_projectpackagealtereddesigninfo aInner Join (SelectParentID,Max(Altertime)'Altertime' fromBid_projectpackagealtereddesigninfoGroup byParentID) b onA.parentid=B.parentid andA.altertime=B.altertime/*You cannot filter out ParentID null.*/SelectA.* fromBid_projectpackagealtereddesigninfo awhere not exists ( Select 1 fromBid_projectpackagealtereddesigninfo bwhereB.parentid=A.parentid andB.altertime>a.altertime)/*2 Using Window functions*//*You cannot filter out ParentID null.*/Select * from(SelectParentid,altertime,Max(Altertime) Over(Partition byParentID) asAltertimemax frombid_projectpackagealtereddesigninfo) xwhereAltertimemax=Altertime
SQL Server takes the latest record in a recent group