具有MAX日期的SELECT行

时间:2017-07-03 07:51:41

标签: sql sql-server greatest-n-per-group

我想选择具有最新日期的ROW。我似乎无法同时选择ID和已发布。

T.Released = MAX(T.Released)对我不起作用。我正在使用MS SQL

{{1}}

2 个答案:

答案 0 :(得分:1)

您必须在子查询中包含该列;

SELECT ID , Released
FROM ( SELECT [id]   
      ,[Released]       
  FROM [dbo].[PressReleases]
  where id   in 
    (
      .....
    ) ) as T    
Where T.Released = MAX (T.Released)

或者你可以这样做;

SELECT ID , Released
FROM [dbo].[PressReleases]
  where id   in 
    (
      .....
    )
AND T.Released = (SELECT MAX (Released) FROM dbo.PressReleases)

答案 1 :(得分:0)

只需在子查询中选择MAX Released-Date。

SELECT [id]   
      ,[Released]       
  FROM [dbo].[PressReleases]
 WHERE Released = ( 
                     SELECT MAX(Released)
                       FROM [dbo].[PressReleases]
                       -- Select here your T Table with the Where Conditions
                  )