如何获得过去 X 天每天的最高金额?
这可能是一件容易的事,但对于我来说,我似乎无法弄清楚。
这是我的表格:
Date User Amount
---------- ----- ------
01/01/2010 User1 2
01/01/2010 User2 2
01/01/2010 User1 4
01/01/2010 User2 1
01/02/2010 User2 2
01/02/2010 User1 2
01/02/2010 User2 4
01/02/2010 User2 1
过去几个月都是如此。我需要得到以下结果:
Date User Amount
---------- ----- ------
01/01/2010 User1 6
01/02/2010 User2 7
基本上,用户每天都有 Max(SUM(Amount))
。
我将不胜感激你们能提供的任何提示。
谢谢。
This is probably a easy one, but for the life of me I can't seem to figure it out.
Here is my table:
Date User Amount
---------- ----- ------
01/01/2010 User1 2
01/01/2010 User2 2
01/01/2010 User1 4
01/01/2010 User2 1
01/02/2010 User2 2
01/02/2010 User1 2
01/02/2010 User2 4
01/02/2010 User2 1
So on for past several months. I need get the following results:
Date User Amount
---------- ----- ------
01/01/2010 User1 6
01/02/2010 User2 7
Basically, the user with Max(SUM(Amount))
for each day.
I would appreciate any hints you guys can offer.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
注意:如果在某一天有两个用户的最大金额相同,这将为您提供两个用户记录。
Note: this will give you both user records if there are two users with the same max amount on a given day.
我实际上最终选择了以下内容:
I actually ended up going with the following:
使用
RANK ... OVER
可以不那么冗长,但以下是直接的解决方案:应该提到的是,如果多个用户具有相同的最大金额,则将显示所有用户。如果不希望出现这种情况,则应使用基于
RANK
的解决方案。Can be less verbose with the
RANK ... OVER
, but following is the straight-forward solution:It should be mentioned that if more then one user has the same maximum amount, all of them will be shown. If this is not desired then one should use
RANK
based solution.使用 CTE,您可以执行以下操作:
非 CTE 解决方案是:
Using CTEs you could do something like:
A non-CTE solution would be: