1

我该如何执行以下操作:

Christopher Vestal 240 $506.00 11/23/2012 $0.91

Christopher Vestal 240 $43.10 11/23/2012 $0.91

Christopher Vestal 240 $77.60 11/23/2012 $0.91

Christopher Vestal 240 $16.00 11/23/2012 $0.91

我检查金额并保留最高金额的费用并将其他金额更改为 0.00 美元。

C Scott Gobble 444 $29.30 11/23/2012 12:00:00 AM $0.00

C Scott Gobble 444 $104.00 11/23/2012 12:00:00 AM $0.00

C Scott Gobble 444 $8.60 11/23/2012 12:00:00 AM $0.00

C Scott Gobble 444 $506.00 11/23/2012 12:00:00 AM $0.91

这是我需要在 SQL 中输出的最终结果。我怎样才能做到这一点?

4

2 回答 2

1

使用 Access 2007,我将Christopher示例数据存储在名为YourTable

full_name          some_number amount  date_field fee
Christopher Vestal         240 $506.00 11/23/2012 $0.91
Christopher Vestal         240  $43.10 11/23/2012 $0.91
Christopher Vestal         240  $77.60 11/23/2012 $0.91
Christopher Vestal         240  $16.00 11/23/2012 $0.91

此查询产生以下结果集。

SELECT
    y.full_name,
    y.some_number,
    y.amount,
    y.date_field,
    IIf(y.amount < DMax(
            "amount",
            "YourTable",
            "full_name ='" & y.full_name & "'"
            ), 0, y.fee
        ) AS adjusted_fee
FROM YourTable AS y;

full_name          some_number amount  date_field adjusted_fee
Christopher Vestal         240 $506.00 11/23/2012        $0.91
Christopher Vestal         240  $43.10 11/23/2012        $0.00
Christopher Vestal         240  $77.60 11/23/2012        $0.00
Christopher Vestal         240  $16.00 11/23/2012        $0.00

如果您需要实际更改存储的fee值,可以使用此UPDATE语句来完成。

UPDATE YourTable AS y
SET y.fee = 0
WHERE
    y.amount < DMax(
            "amount",
            "YourTable",
            "full_name ='" & y.full_name & "'"
            );
于 2013-01-17T22:43:55.073 回答
0

在你的 SQL 语句中尝试使用这样的东西:

 Select iif(Amount = Max(Amount), Fee, 0)
 From MyTable
于 2013-01-17T21:41:27.543 回答