Firebird 按总和和 select 最大值分组

Firebird group by with sum and select max value

Table 命名为:tbl_A

cname       emp          amount
client1     employeeA    100
client2     employeeA    500
client3     employeeA    200

结果应该是

cname       emp          amount
client2     employeeA    800

原因:client2 的 amount 最大值,800 是所有 clients 中 employeeA 的总和。

我试过使用这个代码

select cname, emp, sum(amount)
from tbl_A 
group by cname, emp

它给了我不同的结果。

如果金额与其他客户端相同,那么它将select第一个。

是否可以在一行结果中完成?

好了:

SELECT
  temp.cname,
  temp.emp,
  (SELECT
     SUM(amount)
   FROM tbl_a
   WHERE emp = temp.emp)
FROM (SELECT FIRST 1
        cname,
        emp
      FROM tbl_a
      ORDER BY amount DESC) as temp

更新

SELECT
  emp,
  MAX(cname) as cname,
  (SELECT SUM(tbl_a.amount)
   FROM tbl_a
   WHERE tbl_a.emp = temp.emp)
FROM tbl_a as temp
GROUP BY 1