2017-07-03 90 views
1

我想選擇具有最新日期的ROW。我似乎無法選擇ID和發佈。選擇有MAX日期的行

T.Released = MAX(T.Released)不適用於我。我正在使用MS SQL

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

Where T.Released = MAX (T.Released) 

id Released 
3 2016-05-26 19:30:41.100 
214 2016-07-04 10:10:32.500 
244 2017-01-20 12:20:29.900 
247 2017-03-09 15:50:38.600 
235 2017-06-01 18:10:38.900 
237 NULL 
+0

這是MS SQL :) – SW2702

回答

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 
       ) 
+0

是的,這對我來說很有用,因爲發佈的日期總是獨一無二的。也許不是做這件事的最好方式,但它適用於我 – SW2702

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) 
+0

你好,我想了MAX在T發佈不dbo.PressReleases的也許更多的版本,但我只是想MAX結果T.非常感謝你的回覆 – SW2702