日期:2014-05-18  浏览次数:20601 次

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

ALTER VIEW [dbo].[GetAllUser]
AS
SELECT     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.DisableTime
FROM         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.UID
GO



------解决方案--------------------
--假设按照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)