sql获取最新日期并显示列

时间:2019-01-30 10:39:56

标签: sql sql-server

我只想显示最近日期的1条记录,并显示其他列

 .rightButton {
        flex-grow: 0;
        position: absolute;
        right: 3em;
        z-index: 0;
        float: right;
        width: 2em;
        padding: 3px 3px 4px 0;
        border-bottom: 1px solid #bcbcbc;
        background-color: #fff;
    }

我要的只是显示,因为这是最新记录

lastname firstname datestarted   position               salary
lian     nez       2014-07-05    Cad Operator           4000
lian     nez       2015-06-02    Foreman                10000
lian     nez       2013-04-05    Manpower controller    13000

查询为

lastname firstname datestarted   position               salary
lian     nez       2015-06-02    Foreman            10000

2 个答案:

答案 0 :(得分:1)

您可以使用RANK()

SELECT TOP (1) WITH TIES s.lastname, s.firstname, s1.datestarted, s1.position, s1.salary
FROM constructiondb.dbo.employeetb s INNER JOIN
     constructiondb.dbo.eassignmenttb s1
     ON s1.IDNo = s.IDNo AND s1.projectid = s.projectid
ORDER BY RANK() OVER (PARTITION BY s.IDNo, s.projectid ORDER BY s1.datestarted DESC);

答案 1 :(得分:0)

这将为您提供帮助:

SELECT TOP 1 s.lastname, s.firstname, s1.datestarted, s1.position, s1.salary
FROM constructiondb.dbo.employeetb s 
INNER JOIN constructiondb.dbo.eassignmenttb s1 ON s1.IDNo = s.IDNo AND s1.projectid = s.projectid
ORDER BY s1.datestarted DESC;