我有一个MySQL表,其中表有四列状态,即Dr_amount,Cr_amount,Type,我想知道每个状态的Dr_amount和Cr_amount的总和,其中有两个不同的条件,其中type = in和type = out
我尝试使用以下代码,但正在计算列的每个条目
select *,(select sum(Credit_Rs) from expense where type = 'Received') as crsum,(select sum(Debit_Rs) from expense where type = 'Expense') as drsum from expense group by state order by state
下面是带有示例条目的表格
Month Debit (Rs) Credit (Rs) Type
Bihar 150000 IN
Bihar 50000 50000 OUT
Bihar 15000 10000 OUT
UP 0 100000 IN
我想要这样的结果
Month Debit (Rs) Credit (Rs)
Bihar 65000 150000
UP 0 100000
答案 0 :(得分:0)
您可以尝试使用case when expression
select state,sum(Debit_Rs),sum(case when Type='IN' then Credit_Rs end)
from tablename
group by state
order by state