如何获得过去 X 天每天的最高金额?

发布于 2024-08-25 05:48:10 字数 562 浏览 7 评论 0原文

这可能是一件容易的事,但对于我来说,我似乎无法弄清楚。

这是我的表格:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

真心难拥有 2024-09-01 05:48:10
SELECT MAX(amt),`Date`,`User` FROM
    (SELECT SUM(`Amount`),`Date`,`User` as amt .... GROUP BY `Date`,`User`)
GROUP BY `Date`
SELECT MAX(amt),`Date`,`User` FROM
    (SELECT SUM(`Amount`),`Date`,`User` as amt .... GROUP BY `Date`,`User`)
GROUP BY `Date`
墨小墨 2024-09-01 05:48:10
select t.*
from (
    select Date, Max(Amount) as MaxAmount
    from MyTable
    group by Date           
) tm
inner join MyTable t on tm.Date = t.Date and tm.MaxAmount = t.Amount

注意:如果在某一天有两个用户的最大金额相同,这将为您提供两个用户记录。

select t.*
from (
    select Date, Max(Amount) as MaxAmount
    from MyTable
    group by Date           
) tm
inner join MyTable t on tm.Date = t.Date and tm.MaxAmount = t.Amount

Note: this will give you both user records if there are two users with the same max amount on a given day.

沦落红尘 2024-09-01 05:48:10

我实际上最终选择了以下内容:

WITH ranked AS
(
  SELECT ROW_NUMBER() OVER (ORDER BY SUM(Amount), Date, User) as 'rank', SUM(Amount) AS Amount, User, Date FROM MyTable GROUP BY Date, User
)
SELECT Date, User, Amount
FROM ranked
WHERE rank IN ( select MAX(rank) from ranked group by Date)
ORDER BY Date DESC

I actually ended up going with the following:

WITH ranked AS
(
  SELECT ROW_NUMBER() OVER (ORDER BY SUM(Amount), Date, User) as 'rank', SUM(Amount) AS Amount, User, Date FROM MyTable GROUP BY Date, User
)
SELECT Date, User, Amount
FROM ranked
WHERE rank IN ( select MAX(rank) from ranked group by Date)
ORDER BY Date DESC
晨光如昨 2024-09-01 05:48:10

使用 RANK ... OVER 可以不那么冗长,但以下是直接的解决方案:

WITH summary_user_date 
AS (SELECT      Date, User, SUM(Amount) AS SumAmount
    FROM        MyTable
    GROUP BY    Date, User
)
,   summary_date
AS (SELECT      Date, MAX(SumAmount) AS SumAmount
    FROM        summary_user_date 
    GROUP BY    Date
)
SELECT      summary_user_date.*
FROM        summary_user_date 
INNER JOIN  summary_date
        ON  summary_date.Date = summary_user_date.Date
        AND summary_date.SumAmount = summary_user_date.SumAmount

应该提到的是,如果多个用户具有相同的最大金额,则将显示所有用户。如果不希望出现这种情况,则应使用基于 RANK 的解决方案。

Can be less verbose with the RANK ... OVER, but following is the straight-forward solution:

WITH summary_user_date 
AS (SELECT      Date, User, SUM(Amount) AS SumAmount
    FROM        MyTable
    GROUP BY    Date, User
)
,   summary_date
AS (SELECT      Date, MAX(SumAmount) AS SumAmount
    FROM        summary_user_date 
    GROUP BY    Date
)
SELECT      summary_user_date.*
FROM        summary_user_date 
INNER JOIN  summary_date
        ON  summary_date.Date = summary_user_date.Date
        AND summary_date.SumAmount = summary_user_date.SumAmount

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.

初见终念 2024-09-01 05:48:10

使用 CTE,您可以执行以下操作:

With DailyTotals As
    (
    Select [Date], [User], Sum(Amount) As Total
    From #Test
    Group By [Date], [User]
    )
Select [Date],[User],Total
From DailyTotals As DT
Where Total = (
                Select Max(Total)
                From DailyTotals As DT1
                Where DT1.[Date] = DT.[Date]
                )
Order By DT.[Date]

非 CTE 解决方案是:

Select [Date],[User],Total
From    (
        Select [Date], [User], Sum(Amount) As Total
        From #Test
        Group By [Date], [User]
        ) As DT
Where DT.Total =    (
                    Select Max(DT1.Total)
                    From    (       
                            Select [Date], [User], Sum(Amount) As Total
                            From #Test
                            Group By [Date], [User]
                            ) As DT1
                    Where DT1.[Date] = DT.[Date]
                    )
Order By DT.[Date]

Using CTEs you could do something like:

With DailyTotals As
    (
    Select [Date], [User], Sum(Amount) As Total
    From #Test
    Group By [Date], [User]
    )
Select [Date],[User],Total
From DailyTotals As DT
Where Total = (
                Select Max(Total)
                From DailyTotals As DT1
                Where DT1.[Date] = DT.[Date]
                )
Order By DT.[Date]

A non-CTE solution would be:

Select [Date],[User],Total
From    (
        Select [Date], [User], Sum(Amount) As Total
        From #Test
        Group By [Date], [User]
        ) As DT
Where DT.Total =    (
                    Select Max(DT1.Total)
                    From    (       
                            Select [Date], [User], Sum(Amount) As Total
                            From #Test
                            Group By [Date], [User]
                            ) As DT1
                    Where DT1.[Date] = DT.[Date]
                    )
Order By DT.[Date]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文