0

我有一个场景,其中有 2 个表 tblMember 有 2 列 MemberID 和 MemberName

100  Aakash
101  Seema

tblLoanHistory 列 LoanID、MemberID、FineCharged

1   100   30
2   100   60
3   101   30

我想找到总罚款最高的会员。

输出应该是

100   Aakash   90   (60+30=90)

有人可以帮忙吗

提前致谢。

4

2 回答 2

2
SELECT TOP 1
   M.MemberID, M.MemberName,
   SUM(LH.FineCharged)
FROM
   tblMember  M
   JOIN
   tblLoanHistory LH ON LH.MemberID=M.MemberID 
GROUP BY
   M.MemberID, M.MemberName
ORDER BY
   SUM(LH.FineCharged) DESC

更新...哎呀!添加了 JOIN 条件

于 2013-01-17T14:27:28.593 回答
1
SELECT M.MemberID ,M.MemberName,SUM(LH.FineCharged) FineCharged
FROM tblMember M
INNER JOIN tblLoanHistory LH ON LH.MemberID=M.MemberID 
GROUP BY M.MemberID, M.MemberName
于 2013-01-17T14:47:49.117 回答