SQL语句去除重复数据并取各ID的最后一条数据
去除表中重复数据并取各ClientID的最后一条数据selectBeforeLevel,AfterLevel,ClientID,SaleClientID,ClientNa...
去除表中重复数据并取各ClientID的最后一条数据
select BeforeLevel,AfterLevel,ClientID,SaleClientID,ClientName,Car,MobilePhone,SC.AddTime,Source,PayType,
LoseCar,SaleName,reason,LoseTime,(select count(levelID) FROM SaleClient where levelID=1) as loseNum
FROM LevelChanges as LC left join SaleClient as SC on LC.[SaleClientID]=SC.[ID]
left join Client as CT on SC.[ClientID]=CT.[ID]
where levelID=1 and AfterLevel='P级'
ORDER BY LC.ID,ChangeTime DESC
怎么改? 展开
select BeforeLevel,AfterLevel,ClientID,SaleClientID,ClientName,Car,MobilePhone,SC.AddTime,Source,PayType,
LoseCar,SaleName,reason,LoseTime,(select count(levelID) FROM SaleClient where levelID=1) as loseNum
FROM LevelChanges as LC left join SaleClient as SC on LC.[SaleClientID]=SC.[ID]
left join Client as CT on SC.[ClientID]=CT.[ID]
where levelID=1 and AfterLevel='P级'
ORDER BY LC.ID,ChangeTime DESC
怎么改? 展开
2个回答
2013-06-27
展开全部
先把表倒叙排一下,再用distinct 去除重复 ,distinct 加在你要去除重复的字段前面 distinct 一般取的所有重复数据的第一条,你先把表倒叙了之后再查,第一条就你要的重复数据的最后一条的数据了
2013-06-27
展开全部
distinct 可以去重复,对你这种情况好像无效,你可以直接取 ID或者时间最靠后面的取出来 。这样就能达到你的目地
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询