显示总和的最大值

发布于 2024-11-09 11:39:41 字数 1792 浏览 0 评论 0原文

我试图找到总和结果的最大值,但我也找不到显示日期的方法。 我有一个表 transactions,其中包含如下数据:

transaction_date--Transaction_agent_id--transaction_amount
2/5/11                         321      20
2/5/11                         321      10
2/5/11                         400      100
3/5/11                         321      5
3/5/11                         345      100
3/5/11                         321      10
3/5/11                         345      50
4/5/11                         345      80
4/5/11                         321      5

我想要的结果是这样

transaction_date--Transaction_agent_id--transaction_amount
    2/5/11                       321     30
    2/5/11                       400    100
    3/5/11                       345    150

它应该做的是首先将每个 agent_id 每天的所有金额相加,然后向我显示该代理的最佳日期。

我所做的是这

SELECT a.transaction_agent_id, max(a.stotal) FROM
(
SELECT transaction_date,transaction_agent_id, sum(transaction_amount) as 'Stotal' FROM transactions
GROUP BY transaction_agent_id, transaction_date
)a
GROUP BY a.transaction_agent_id

给了我这个,

Transaction_agent_id--transaction_amount
               321     30
               400    100
               345    150

这是正确的,但我无法添加 transaction_date。

更新:我刚刚解决了。好吧,如果有人想要解决方案,那就是这里。

SELECT  a.transaction_date, a.transaction_agent_id, max(a.stotal) FROM
(
  SELECT row_number() over (partition by transaction_agent_id order by sum(transaction_amount)desc) AS 'roww' ,transaction_date,transaction_agent_id, sum(transaction_amount) as 'Stotal' FROM transactions
  GROUP BY transaction_agent_id, transaction_date
) a

WHERE a.roww = 1
GROUP BY a.transaction_date, a.transaction_agent_id, a.roww

感谢大家的意见。

i am trying to find the max values of a sum result but i can not find a way to show the date too.
i have a table transactions which has data like this:

transaction_date--Transaction_agent_id--transaction_amount
2/5/11                         321      20
2/5/11                         321      10
2/5/11                         400      100
3/5/11                         321      5
3/5/11                         345      100
3/5/11                         321      10
3/5/11                         345      50
4/5/11                         345      80
4/5/11                         321      5

what i want the result to be is this

transaction_date--Transaction_agent_id--transaction_amount
    2/5/11                       321     30
    2/5/11                       400    100
    3/5/11                       345    150

What it should do is first of all to sum all the amount per agent_id, per day and then to show me the best day of that agent.

What i have done is this

SELECT a.transaction_agent_id, max(a.stotal) FROM
(
SELECT transaction_date,transaction_agent_id, sum(transaction_amount) as 'Stotal' FROM transactions
GROUP BY transaction_agent_id, transaction_date
)a
GROUP BY a.transaction_agent_id

which gives me this

Transaction_agent_id--transaction_amount
               321     30
               400    100
               345    150

which is correct but i can not add the transaction_date.

Update: I just solved it. Well if anyone wants the solution here it is.

SELECT  a.transaction_date, a.transaction_agent_id, max(a.stotal) FROM
(
  SELECT row_number() over (partition by transaction_agent_id order by sum(transaction_amount)desc) AS 'roww' ,transaction_date,transaction_agent_id, sum(transaction_amount) as 'Stotal' FROM transactions
  GROUP BY transaction_agent_id, transaction_date
) a

WHERE a.roww = 1
GROUP BY a.transaction_date, a.transaction_agent_id, a.roww

Thank you all for your input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

妞丶爷亲个 2024-11-16 11:39:41

SQL语句

;WITH Max_Transaction_Amount AS (
    SELECT  Transaction_Date
            , Transaction_Agent_ID
            , Transaction_Amount = SUM(Transaction_Amount)
    FROM    q
    GROUP BY
            Transaction_Date
            , Transaction_Agent_ID
)
SELECT  mta.Transaction_Date
        , mta.Transaction_Agent_ID
        , mta.Transaction_Amount
FROM    Max_Transaction_Amount mta 
        INNER JOIN (
            SELECT  Transaction_Agent_ID
                    , Transaction_Amount = MAX(Transaction_Amount)
            FROM    Max_Transaction_Amount
            GROUP BY
                    Transaction_Agent_ID        
        ) q ON  mta.Transaction_Agent_ID = q.Transaction_Agent_ID
                AND mta.Transaction_Amount = q.Transaction_Amount

测试脚本

/* Create test data */
;WITH q (Transaction_Date, Transaction_Agent_ID, Transaction_Amount) AS (
  SELECT '2/5/11',321,20
  UNION ALL SELECT '2/5/11',321,10
  UNION ALL SELECT '2/5/11',400,100
  UNION ALL SELECT '3/5/11',321,5
  UNION ALL SELECT '3/5/11',345,100
  UNION ALL SELECT '3/5/11',321,10
  UNION ALL SELECT '3/5/11',345,50
  UNION ALL SELECT '4/5/11',345,80
  UNION ALL SELECT '4/5/11',321,5
)
/* Actual query */
, Max_Transaction_Amount AS (
    SELECT  Transaction_Date
            , Transaction_Agent_ID
            , Transaction_Amount = SUM(Transaction_Amount)
    FROM    q
    GROUP BY
            Transaction_Date
            , Transaction_Agent_ID
)
SELECT  mta.Transaction_Date
        , mta.Transaction_Agent_ID
        , mta.Transaction_Amount
FROM    Max_Transaction_Amount mta 
        INNER JOIN (
            SELECT  Transaction_Agent_ID
                    , Transaction_Amount = MAX(Transaction_Amount)
            FROM    Max_Transaction_Amount
            GROUP BY
                    Transaction_Agent_ID        
        ) q ON  mta.Transaction_Agent_ID = q.Transaction_Agent_ID
                AND mta.Transaction_Amount = q.Transaction_Amount

SQL Statement

;WITH Max_Transaction_Amount AS (
    SELECT  Transaction_Date
            , Transaction_Agent_ID
            , Transaction_Amount = SUM(Transaction_Amount)
    FROM    q
    GROUP BY
            Transaction_Date
            , Transaction_Agent_ID
)
SELECT  mta.Transaction_Date
        , mta.Transaction_Agent_ID
        , mta.Transaction_Amount
FROM    Max_Transaction_Amount mta 
        INNER JOIN (
            SELECT  Transaction_Agent_ID
                    , Transaction_Amount = MAX(Transaction_Amount)
            FROM    Max_Transaction_Amount
            GROUP BY
                    Transaction_Agent_ID        
        ) q ON  mta.Transaction_Agent_ID = q.Transaction_Agent_ID
                AND mta.Transaction_Amount = q.Transaction_Amount

Test script

/* Create test data */
;WITH q (Transaction_Date, Transaction_Agent_ID, Transaction_Amount) AS (
  SELECT '2/5/11',321,20
  UNION ALL SELECT '2/5/11',321,10
  UNION ALL SELECT '2/5/11',400,100
  UNION ALL SELECT '3/5/11',321,5
  UNION ALL SELECT '3/5/11',345,100
  UNION ALL SELECT '3/5/11',321,10
  UNION ALL SELECT '3/5/11',345,50
  UNION ALL SELECT '4/5/11',345,80
  UNION ALL SELECT '4/5/11',321,5
)
/* Actual query */
, Max_Transaction_Amount AS (
    SELECT  Transaction_Date
            , Transaction_Agent_ID
            , Transaction_Amount = SUM(Transaction_Amount)
    FROM    q
    GROUP BY
            Transaction_Date
            , Transaction_Agent_ID
)
SELECT  mta.Transaction_Date
        , mta.Transaction_Agent_ID
        , mta.Transaction_Amount
FROM    Max_Transaction_Amount mta 
        INNER JOIN (
            SELECT  Transaction_Agent_ID
                    , Transaction_Amount = MAX(Transaction_Amount)
            FROM    Max_Transaction_Amount
            GROUP BY
                    Transaction_Agent_ID        
        ) q ON  mta.Transaction_Agent_ID = q.Transaction_Agent_ID
                AND mta.Transaction_Amount = q.Transaction_Amount
兮子 2024-11-16 11:39:41

只需在外部选择中按 transaction_date 进行分组即可:

SELECT a.transaction_date, a.transaction_agent_id, max(a.stotal) FROM
(
  SELECT transaction_date,transaction_agent_id, sum(transaction_amount) as 'Stotal' FROM transactions
  GROUP BY transaction_agent_id, transaction_date
) a
GROUP BY a.transaction_agent_id, a.transaction_date

Just group by transaction_date in the outer select as well:

SELECT a.transaction_date, a.transaction_agent_id, max(a.stotal) FROM
(
  SELECT transaction_date,transaction_agent_id, sum(transaction_amount) as 'Stotal' FROM transactions
  GROUP BY transaction_agent_id, transaction_date
) a
GROUP BY a.transaction_agent_id, a.transaction_date
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文