1
=======================================
| 输入类型 | 应付 | 金额 | 标识符 |
=======================================
| 费用 | 10,000 | 5,000 | 1 |
--------------------------------------
| 费用 | 10,000 | 5,000 | 1 |
--------------------------------------
| 费用 | 10,000 | 5,000 | 2 |
--------------------------------------
| 存款 | 5,000 | 4,000 | 1 |
--------------------------------------
| 存款 | 15,000 | 10,000 | 2 |
--------------------------------------

如果我想从上表中查看这样的余额:

===============================
| 标识符 | 余额1 | 余额2 |
===============================
| 1 | 24,000 | 15,000 |
------------------------------
| 2 | 20,000 | 20,000 |
------------------------------

pID 1 的计算:

应付(费用)10,000+10,000 + 金额(存款)4,000 = 余额1 24,000

应付(费用)5,000+5,000 + 金额(存款)5,000 = 余额2 15,000

pID 2 的计算:

应付(费用)10,000 + 金额(存款)10,000 = 余额1 20,000

应付(费用)5,000 + 金额(存款)15,000 = 余额2 20,000

  • 获取数据后如何进行计算?
4

1 回答 1

1

如果 InputType 只能有两种不同的状态,我认为您正在寻找这样的东西:

select
  pID,
  sum(case when InputType='Expenses' then Payable else Amount end) as Balance1,
  sum(case when InputType='Expenses' then Amount else Payable end) as Balance2
from tbl
group by pID
于 2012-12-04T11:43:37.037 回答