【问题标题】:SQL ACCESS - select max(date) and corresponding valueSQL ACCESS - 选择 max(date) 和相应的值
【发布时间】:2018-02-16 02:57:42
【问题描述】:

如何获取MAX(date)的对应值。 当我直接选择具有指定值的列时,访问它会返回错误。

例如,我只想显示图像中的线条。

谢谢。

【问题讨论】:

  • 如果有两行的最大日期相同,预期的结果是什么?

标签: sql date ms-access select max


【解决方案1】:

使用TOPORDER BY

select top 1 *
from t
order by date desc;

编辑:

如果您想要每个代码的最后日期,请使用相关子查询:

select t.*
from t
where t.date = (select max(t2.date) from t t2 where t2.code = t.code);

【讨论】:

  • 谢谢,但这只会返回整个数据库最后日期的交易。我需要每个代码的最后日期。
【解决方案2】:

select * from tblName where DocumentDate in (select max(DocumentDate ) from tblName)

请使用这个

【讨论】:

    【解决方案3】:

    您创建连接查询。例如查找MAX(DocumentDate):

    SELECT DocumentNumber, Code, SoldPuncte, DocumentDate   
    from yourTable a inner join 
              (SELECT DocumentNumber, Code, SoldPuncte, MAX(DocumentDate) as 
              DocumentDate 
              from yourTable group by DocumentNumber) b 
    on a.DocumentNumber=b.DocumentNumber and a.DocumentDate = b.DocumentDate
    

    【讨论】:

      【解决方案4】:

      如果您需要每个代码的最后日期,那么试试这个

      SELECT Code, MAX(DocumentDate)
      FROM table
      GROUP BY Code
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-30
        • 2019-01-31
        • 2013-06-25
        • 1970-01-01
        • 2015-12-10
        • 2017-11-24
        • 1970-01-01
        相关资源
        最近更新 更多