显示已通过其所有包裹交付订单向最大费用贡献的客户的详细信息
我有一个客户表: 顾客 客户编号客户名称客户地址
另一项业务 BusinessId名称收取CustomerId
一个客户可能有很多收费,我们需要对每个客户收费进行汇总,找到这些累计费用中的最大值,并显示已支付滞纳金的
SELECT customerid,
MAX(scharge)
FROM
(
SELECT
customerid, SUM(charges) as scharge
FROM parcel
group by customerid
)
group by customerid
答案 0 :(得分:0)
使用TOP
和ORDER BY
:
SELECT TOP 1 c.*
FROM (SELECT customerid, SUM(charges) as scharge
FROM parcel
GROUP BY customerid
) c
ORDER BY scharge DESC;