如果我们有以下数据,我想使用“SomeID”列或“ApplicationID”获取数据。因此,返回的数据基本上是基于“SomeID”
的单独列中的金额 ID SomeID ApplicationID Amount
19 8 19 45.18
20 8 20 45.18
21 8 21 225.91
22 8 22 203.32
72 10 19 45.18
73 10 20 45.18
74 10 21 225.91
75 10 22 203.32
我想返回
Last Month repayment This Month repaymnrt Variance
45.18 45.18 0
45.18 45.18 0
225.91 225.91 0
203.32 203.32 0
答案 0 :(得分:1)
我认为您可以使用pivot
或条件聚合来执行您想要的操作:
select applicationId,
sum(case when someId = 8 then Amount else 0 end) as LastMonth,
sum(case when someId = 10 then Amount else 0 end) as ThisMonth,
sum(case when someId = 8 then -Amount
when someId = 10 then Amount
else 0
end) as Diff
from t
group by applicationId;