仅显示最长日期

时间:2016-10-20 15:40:32

标签: sql sql-server

我想获得2999-01-01的订单号1628481输出。我使用Group By命令和聚合函数MAX()作为日期,它显示所有行。

enter image description here

1 个答案:

答案 0 :(得分:0)

I cannot tell exactly what your primary key is, but that is the only thing missing from the code.

Select s.*
From dbo.PUK_Status s
INNER JOIN (
SELECT [Whatever the PK is], MAX(ENDDATE) maxdate
from PUK_STATUS 
WHERE PUK = '1628481'
) maxs
on s.[Whatever the PK is] = maxs.[Whatever the PK is]
and s.ENDDATE = maxs.ENDDATE