首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

在这个视图中时按时间查询重复数据中最新数据,其余数据不变!求解!该怎么解决

2012-03-30 
在这个视图中时按时间查询重复数据中最新数据,其余数据不变!求解!SQL codeALTER VIEW [dbo].[GetAllUser]A

在这个视图中时按时间查询重复数据中最新数据,其余数据不变!求解!

SQL code
ALTER VIEW [dbo].[GetAllUser]ASSELECT     Users_1.UID, Users_1.Email, Users_1.Mobile, Users_1.UserID, Users_1.PassWord, Users_1.UserRank, Users_1.IsState, Users_1.IsVIP,dbo.UserInfo.CreateTime,                      CASE WHEN charindex('.', '' + r.RUID + '') > 0 THEN replace(r.RUID, 'http://', '') WHEN r.RUID IS NULL THEN '--' ELSE                          (SELECT     UserID                            FROM          dbo.Users                            WHERE      (UID = r.RUID)) END AS RUserID, v.ValPhone, dbo.UserInfo.QQ,udb.Reasons,udb.DisableTimeFROM         dbo.Users AS Users_1 INNER JOIN                      dbo.UserInfo ON Users_1.UID = dbo.UserInfo.UID LEFT OUTER JOIN                      dbo.ReferrerInfo AS r ON Users_1.UID = r.UID LEFT OUTER JOIN                      dbo.UserValidation AS v ON Users_1.UID = v.UID LEFT OUTER JOIN                      dbo.UsersDisable as udb on Users_1.UID=udb.UIDGO


[解决办法]
--假设按照UID取最新的CreateTime

select t.* from [dbo].[GetAllUser] t where CreateTime = (select max(CreateTime) from [dbo].[GetAllUser] where UID = t.UID)

select t.* from [dbo].[GetAllUser] t where not exists (select 1 from [dbo].[GetAllUser] where UID = t.UID and CreateTime > t.CreateTime)

热点排行