【问题标题】:SQLite How to calculate a difference between two variables of the same table [closed]SQLite如何计算同一张表的两个变量之间的差异[关闭]
【发布时间】:2021-02-04 05:41:07
【问题描述】:

我对 SQL 很陌生,考虑到下表,我不知道如何计算储藏室中每项资产的金额 (in_amount - out_amount)。

理想情况下我会得到 this result table (calculation between parentheses for clarification) 感谢您的帮助!

【问题讨论】:

  • 编辑您的问题并显示您想要的结果。问题不清楚。

标签: sql sqlite sum aggregate-functions unpivot


【解决方案1】:

这看起来像 union all 来取消透视,然后聚合:

select asset, sum(amount) total_amount
from (
    select in_asset as asset, in_amount as amount from mytable
    union all
    select out_asset, - out_amount from mytable
) t
group by asset

【讨论】:

  • 非常感谢@GMB!正是我想要做的。
猜你喜欢
  • 1970-01-01
  • 2020-11-30
  • 2020-10-16
  • 1970-01-01
  • 2010-10-29
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
  • 2019-03-18
相关资源
最近更新 更多