SQL查询MS Access上的最后一笔交易

时间:2019-11-26 20:18:55

标签: sql ms-access

嗨,我有一个包含以下字段的表格:

  • 客户编号
  • Customerpaydate
  • 金额

我需要帮助来检索最近的交易,但不能重复输入客户ID。

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

要提取最近的交易客户,相关子查询应完成工作:

select t.*
from mytable t
where customerpaydate = (
    select max(Customerpaydate) from mytable t1 where t1.customerid = t.customerid      
)

答案 1 :(得分:0)

这将为您提供最高工资日期

 SELECT MAX(customerpaydate) as d 
 FROM table

这将为您提供客户的最高付款日期

 SELECT customerid, max(customerpaydate) as d 
 FROM table 
 GROUP BY customerid

这将为您提供最新的交易

 SELECT *
 FROM TABLE
 where customerpaydate = (SELECT MAX(customerpaydate) as d FROM table)

这将为您提供每个客户的最新交易

 SELECT *
 FROM TABLE
 JOIN (
    SELECT customerid, max(customerpaydate) as d 
    FROM TABLE 
    GROUP BY customerid
 ) AS S ON S.customerid = TABLE.customerid and S.d = TABLE.customerpaydate