去 账号、昵称重复,并取最新一次绑定记录
代码如下:
select t1.* from (SELECT ut.TransactionAccount,u.NickName, p.ProfessorName '绑定老师', ub.UpdateTime AS '变动时间' FROM dbo.UserBind ub JOIN dbo.UserTransaction ut ON ub.UserId=ut.UserId JOIN dbo.Users u ON ub.UserId=u.UserId JOIN dbo.Professor p ON p.Id=ub.ProfessorId WHERE u.IsEnable=1 AND ub.UpdateTime<'2016-01-18') t1 inner join (SELECT ut.TransactionAccount,u.NickName, max(ub.UpdateTime) AS '变动时间' FROM dbo.UserBind ub JOIN dbo.UserTransaction ut ON ub.UserId=ut.UserId JOIN dbo.Users u ON ub.UserId=u.UserId JOIN dbo.Professor p ON p.Id=ub.ProfessorId WHERE u.IsEnable=1 AND ub.UpdateTime<'2016-01-18' group by ut.TransactionAccount,u.NickName) as t2 on t1.TransactionAccount=t2.TransactionAccount and t1.变动时间=t2.变动时间
写法思路:
有时间列就取最大时间列
有自增的id就去最大id
如果是想返回一个记录集,并且有时间列的话可以这么做
select 重复列,时间列,其余列
inner join (select 重复列,max(时间列) as 时间列 from 表 group by 重复列) AS T2
on t1.重复列=t2.重复列 and t1.时间列 =t2.时间列
from 表 AS T1
总结:
写法代码段重复很多,大家有什么好的方法呢???