【问题标题】:how to get balance of buy and sell when there are different currency?当有不同的货币时如何获得买卖平衡?
【发布时间】:2019-07-20 15:02:10
【问题描述】:

下面的 MySQL 表包含了不同货币的关联金额的买入和卖出,如何根据每个currency 选择买入、卖出和余额的总和?我试过了,但有些东西不能正常工作。

id      currency    buy  change_Currency     sell
1         USD      1000        DR             3670             
2         EURO      100        USD            130
3         DR        500        USD            136
4         USD       500        EURO           600
5         USD       1200        DR            3800

我的 MySQL 查询不起作用:

SELECT currency,SUM(buy)-SUM(sell) AS balance FROM deal GROUP BY currency,change_currency

我想要的结果是这样的:

currency    buy     sell    balance
  USD       2700    266      2434 
  DR        500     7470     -6970      
 EURO       100     600      -500 

我不知道还能尝试什么。任何帮助都会很棒。

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    将表格分组两次,一次用于购买,一次用于销售,然后加入结果:

    select 
      t1.currency,
      t1.total buy,
      t2.total sell,
      t1.total - t2.total balance
    from (
      select currency, sum(buy) total from deal
      group by currency 
    ) t1 inner join (
      select change_currency, sum(sell) total from deal
      group by change_currency 
    ) t2 on t2.change_currency = t1.currency
    

    请参阅demo
    结果:

    | currency | buy  | sell | balance |
    | -------- | ---- | ---- | ------- |
    | DR       | 500  | 7470 | -6970   |
    | EURO     | 100  | 600  | -500    |
    | USD      | 2700 | 266  | 2434    |
    

    【讨论】:

    • 谢谢您,您的回答是正确的。我想在 where class like (where date = '7/28/2019') 中使用条件我如何在你的答案中添加这个条件。@forpas
    【解决方案2】:

    仅使用自联接和sum() 聚合分组:

    select t1.currency, 
           sum(t1.buy) as buy,
           sum(t2.sell) as sell,
           sum(t1.buy)-sum(t2.sell) as balance
      from ( select currency, sum(buy) as buy from deal group by currency ) t1
      join ( select change_Currency, sum(sell) as sell from deal group by change_Currency ) t2 
        on t1.Currency=t2.change_Currency
     group by t1.currency
    

    【讨论】:

    • 如果其中一个子查询中缺少货币,则 INNER JOIN 可能会导致行排除。
    • 当然@PaulSpiegel,但我更喜欢这个样本数据和所需的输出。
    【解决方案3】:

    我会使用union all 和聚合。您可以在union all:

    beforeafter 进行聚合
    select currency, sum(buy), sum(sell),
           sum(buy) - sum(sell)
    from ((select currency, sum(buy) as buy, 0 as sell
           from deal
           group by currency
          ) union all
          (select change_currency, 0, sum(sell) as sell
           from deal
           group by change_currency
          )
         ) bs
    group by currency;
    

    如果您有单独的货币表,那么最有效的方法可能是相关子查询:

    select c.currency,
           (select sum(d.buy)
            from deal d
            where d.currency = c.currency
           ) as buy,
           (select sum(d.sell)
            from deal d
            where d.change_currency = c.currency
           ) as sell
    from currencies c;
    

    特别是,这可以利用deal(currency, buy)deal(change_currency, sell) 上的索引。

    【讨论】:

    • 这比 INNER JOIN 的答案更准确,因为它们可以排除仅被买入或卖出的货币。
    • 谢谢您,您的回答是正确的。我想在 where class like (where date = '7/28/2019') 中使用条件我如何在您的第一个答案中添加此条件。
    • @safiullah 。 . .首先,在子查询中的group bys 之前添加两次。
    【解决方案4】:

    这是一种使用UNION 和子查询将指定货币的列放入同一列的方法。 mysql 的语法可能略有偏差,因为我已经多年没有使用它了。

    SELECT currency, sum(buy), sum(sell), sum(buy) - sum(sell) balance FROM 
        (SELECT currency, buy, 0 sell FROM deal
         UNION 
         SELECT change_currency, 0, sell FROM deal) deals
    GROUP BY currency
    ORDER BY currency
    

    【讨论】:

      【解决方案5】:

      尝试做这样的事情

      SELECT
      deal1.currency
      , sum(deal1.buy) as buy
      , sum(deal2.sell) as sell
      , (sum(deal1.buy) - sum(deal2.sell)) as balance
      FROM deal deal1
      join deal deal2
        on deal1.currency = deal2.change_Currency
      GROUP BY deal1.currency
      

      【讨论】:

      • 您不能简单地分组货币,因为卖出货币由change_currency指定。天平会把它们混在一起。
      猜你喜欢
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 2021-10-09
      • 2021-11-02
      • 2021-11-29
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      相关资源
      最近更新 更多