来自Select数据的查询中的最大值

时间:2014-12-08 03:20:48

标签: sql max

我是SQL的新手,需要一些查询帮助。我正在尝试使用以下查询查找MAX TradeCodeID。它没有返回我需要的数据。它几乎回归了。

select distinct 
    t.useremployeeid, 
    max(t.usertradeID), 
    t.Projectfullname, 
    t.userfirstname + ' '+ t.userlastname as GreatestPM
from
   (select distinct 
        users.UserTradeId, UserEmployeeID, UserFirstName, UserLastName, 
        ProjectFullName, ProjectManager, 
        max(ScheduleDate) as LastDate
    from 
        schedules 
    left outer join 
        users on ScheduleUserID = UserID
    left outer join 
        Phases on SchedulePhaseID = PhaseID 
    left outer join 
        Projects on phases.ProjectID = projects.ProjectID
    left outer join 
        UserTrades on UserTrades.UserTradeID = Users.UserTradeID
    where 
        users.useractive = 1 
        and users.useremployeeid <> 0 
        and users.usertradeid between 21 and 24 
        and projectfullname is not null
    group by 
        users.UserTradeid, UserEmployeeID, UserFirstName, UserLastName, 
        ProjectFullName, ProjectManager
    having 
        max(scheduledate) > getdate() ) t
group by 
    t.projectfullname, t.userfirstname,t.userlastname, UserEmployeeID
order by 
    t.projectfullname

从以下数据集中:

useremployeeid  UserTradeID Projectfullname                         GreatestPM
--------------------------------------------------------------------------------
12121              22                    162331.05                   John Smith
25487              21                    166324.1                    Chuck Norris
45639              21                    166324.1                    Brad Pitt
35789              23                    166324.1                    John Doe
15697              24                    166324.1                    Matt Damon
28957              23                    166324.1                    Taylor Swift
76985              21                    166324.1                    Tony Romo
25496              21                    166324.1                    George Strait
85695              22                    167091.1                    Robin Roberts
75632              21                    167091.1                    Scott Smith
66897              22                   1663341.01                   Garth Brooks
58766              21                   1663341.01                   Travis Tritt
37895              21                   1663341.01                   Sara Roberts
95687              21                   1663352.01                   Justin Timberlake
85697              24                   1663352.01                   Sally Walker

我希望得到以下结果:

useremployeeid  UserTradeID Projectfullname GreatestPM
----------------------------------------------------------
12121               22            162331.05 John Smith
15697               24            166324.1  Matt Damon
85695               22            167091.1  Robin Roberts
66897               22           1663341.01 Garth Brooks
85697               24           1663352.01 Sally Walker

感谢您的帮助。

0 个答案:

没有答案