sqlserver 根据某个字段去重
根据某个字段排序分组:
select row_number() over ( PARTITION BY 重复的字段名 ORDER BY 根据主要的ID字段名 DESC ) dsnamesnum
dsnamesnum:表示新增的列名
select * from ( select row_number() over ( PARTITION BY LName ORDER BY PID DESC ) dsnamesnum,* from (
select row_number() over ( PARTITION BY a.PID ORDER BY a.PtID DESC ) distinctnum ,c.Names as pro_Namesfrom table1 a left join table2 c on c.ID=a.Name left join table3 d on d.PID=a.PID left join table4 e on e.AID=d.AID left join table5 f on f.MID=d.MID where d.AID=151 and f.IsLock=0 and f.State=1 ) TanleOne where distinctnum=1 //--上层table里row_number() over 根据倒序查询出来distinctnum的结果是1++,1就是最新数据,所以=1
) TableTwo where dsnamesnum=1 //--上层table里row_number() over 根据倒序查询出来dsnamesnum的结果是1++,1就是最新数据,所以=1