用户
Id UserId Date Amount NewBalance NewTotal
---------------------------------------------------------
1 1 2020/03/21 100 100 100
2 1 2020/04/06 100 200 200
3 1 2020/04/07 25 225 225
4 2 2020/03/03 100 100 100
5 2 2020/04/07 30 130 130
6 3 2020/04/06 250 250 250
交易
newBalance
newTotal
是该日期之前所有金额的总和,UserId UserName Amount_made_this_week
---------------------------------------------
1 3 Jon snow 250
2 1 Robert Baratheon 125
3 2 Cercei Lanister 30
是该日期之前所有阳性金额的总和。
我想订购本周赚钱最多的人
所需结果:
{{1}}
如何用SQL编写?
答案 0 :(得分:1)
您可以总结本周的amount
过滤条件并排序结果:
SELECT Transactions.UserId, Users.Name, Sum(Transactions.Amount) as amount_made_this_week
FROM Transactions
INNER JOIN Users
ON Transactions.UserID = Users.ID
WHERE Transactions.Date BETWEEN #04/05/2020# AND #04/11/2020#
GROUP BY Userid, Name
ORDER BY amount_made_this_week DESC;
答案 1 :(得分:0)
您还可以将日期范围与绑定变量无关,始终设置一个星期的时间,例如:
SELECT T.UserId, U.Name, Sum(T.Amount) as Amount_made_this_week
FROM Transactions T
INNER JOIN Users U
ON T.UserID = U.ID
WHERE T.Date > (SYSDATE-7)
GROUP BY Userid, Name
ORDER BY Amount_made_this_week DESC;