根据条件在Min(字段)上进行MS-Access查询

时间:2017-04-12 15:25:12

标签: sql ms-access

请帮我构建一个SQL查询,根据最短日期和名称查找字段AMT中的最小amt: 我想在ACCESS表中填充字段[Min amt],该表基于对Name的最小日期。例如,名称A的最小日期为4/9/17,其amt为7000:

Name    Date         Amt        Min Date     Min Amt
A       4/12/2017    $1,000.00  4/9/2017     $7,000.00 
A       4/11/2017    $2,000.00  4/9/2017     $7,000.00 
B       4/10/2017    $3,000.00  4/10/2017    $3,000.00 
B       4/11/2017    $4,000.00  4/10/2017    $3,000.00 
C       4/10/2017    $5,000.00  4/10/2017    $5,000.00 
C       4/11/2017    $6,000.00  4/10/2017    $5,000.00 
A       4/9/2017     $7,000.00  4/9/2017     $7,000.00 

1 个答案:

答案 0 :(得分:1)

我建议您实际创建这些计算列。一旦新数据进入,它们很容易被无效,使它们对您无用。

相反,我建议您只创建一个报告,显示每个名称的最早金额:

SELECT t1.Name, t1.Date, t1.Amt,
       t2.[Min Date],
       t2.[Min Amt]
FROM yourTable t1
INNER JOIN
(
    SELECT t1.Name, t1.Date AS [Min Date], t1.Amt AS [Min Amt]
    FROM yourTable t1
    INNER JOIN
    (
        SELECT Name, MIN(Date) AS min_date
        FROM yourTable
        GROUP BY Name
    ) t2
        ON t1.Name = t2.Name AND
           t1.Date = t2.min_date
) t2
    ON t1.Name = t2.Name