SQL如何在已使用max时获取max日期

SQL如何在已使用max时获取max日期,sql,sql-server,Sql,Sql Server,我想得到最大f.post日期,然后是最大a.process日期。 当我尝试使用MAX(a.processdate)时,它将不起作用,因为group by和另一个MAX已经被使用了 SELECT f.PARENTACCOUNT , MAX(f.POSTDATE) AS [Post Date] , a.ProcessDate , s.type , CONCAT(n.FIRST, ' ', n.MIDDLE, ' ', n.LAST) A

我想得到最大f.post日期,然后是最大a.process日期。 当我尝试使用MAX(a.processdate)时,它将不起作用,因为group by和另一个MAX已经被使用了

SELECT f.PARENTACCOUNT
        , MAX(f.POSTDATE) AS [Post Date]
        , a.ProcessDate
        , s.type
        , CONCAT(n.FIRST, ' ', n.MIDDLE, ' ', n.LAST) AS [Member Name]
        , s.BALANCE
FROM dbo.FMHISTORY f
JOIN dbo.ACCOUNT a
    ON f.PARENTACCOUNT = a.ACCOUNTNUMBER
JOIN dbo.NAME n 
    ON f.PARENTACCOUNT = n.PARENTACCOUNT
        AND a.ProcessDate = n.ProcessDate
JOIN dbo.SAVINGS s
    ON f.PARENTACCOUNT = s.PARENTACCOUNT
        AND a.ProcessDate = s.ProcessDate
    where (a.WARNINGCODE1 = 52
        OR a.WARNINGCODE2 = 52
        OR a.WARNINGCODE3 = 52
        OR a.WARNINGCODE4 = 52
        OR a.WARNINGCODE5 = 52
        OR a.WARNINGCODE6 = 52
        OR a.WARNINGCODE7 = 52
        OR a.WARNINGCODE8 = 52
        OR a.WARNINGCODE9 = 52
        OR a.WARNINGCODE10 = 52)
        AND n.TYPE = 0
        AND f.PARENTACCOUNT = '0000123456'
GROUP BY f.PARENTACCOUNT, s.type, s.BALANCE, a.ProcessDate, n.FIRST, n.MIDDLE, n.LAST
ORDER BY f.PARENTACCOUNT
这是我到目前为止的结果:

PARENTACCOUNT   Post Date       ProcessDate   type      Member Name     BALANCE
0000123456  2020-01-24 00:00:00  20180831      1          Jane Doe      12345.04
0000123456  2020-01-24 00:00:00  20180930      1          Jane Doe      12345.12
0000123456  2020-01-24 00:00:00  20181031      1          Jane Doe      12345.23
0000123456  2020-01-24 00:00:00  20181130      1          Jane Doe      12345.31
我希望它只返回这个:

PARENTACCOUNT   Post Date       ProcessDate   type      Member Name     BALANCE
0000123456  2020-01-24 00:00:00  20181130      1          Jane Doe      12345.31

注意:a.processdate是一个INT(我没有这样创建数据库)。

好的,您可以使用
选择top(1)

如果您需要对多个父帐户执行此操作,那么一个简单的方法是:

select top (1) with ties . . .
. . .
order by row_number() over (partition by f.parentaccount, processdate desc)

我在order by row_number()中遇到语法错误。我试着把desc放在括号外,但它说“函数‘row_number’必须有一个带有ORDER BY的OVER子句。”
select top (1) with ties . . .
. . .
order by row_number() over (partition by f.parentaccount, processdate desc)