在一行中获取贷记和借记 sql
Get credit and debit in a single row sql
我无法找到与帐户相关的报告
我需要在一行中获得信用账户和借记账户..金额
我的table是这样的
Id VoucherId AccountId Amount AccountName IsDebit
1 1 26 100 Sales 0
2 1 10 100 Cash 1
3 2 26 200 Sales 0
4 2 10 200 Cash 1
5 3 10 150 Cash 0
6 3 20 150 Expense A 1
7 4 10 240 Cash 0
8 4 21 240 Expense B 1
我需要得到这样的结果
VoucherId DebitName DebitID CreditName CreditID 金额<br>
1 现金 10 销售额 26 100
2 现金 10 销售额 26 200
3 支出 a 10 现金 26 150
4 费用 b 10 现金 26 240
我试过了
select vc.Id,
am.Name,vc.AccountName,vd.[Description],
case when vd.isdebit=1 then vd.amount else 0 end as C6,
case when vd.isdebit=1 then vd.AccountId else 0 end as A,
case when vd.isdebit=0 then vd.amount else 0 end as C7,
case when vd.isdebit=0 then vd.AccountId else 0 end as B
from VoucherDetails vd
inner join Voucher Vc on vc.Id=vd.VoucherId and vc.IsDeleted=0
inner join AccountsMaster Am on am.Id=vd.AccountId
和许多其他查询,但没有得到上述结果
请帮助..提前致谢
您需要聚合:
select vc.Id,
sum(case when vd.isdebit = 1 then vd.amount else 0 end) as C6,
sum(case when vd.isdebit = 1 then vd.AccountId else 0 end) as A,
sum(case when vd.isdebit = 0 then vd.amount else 0 end) as C7,
sum(case when vd.isdebit = 0 then vd.AccountId else 0 end) as B
from VoucherDetails vd join
Voucher Vc
on vc.Id = vd.VoucherId and vc.IsDeleted = 0 join
AccountsMaster Am
on am.Id = vd.AccountId
group by vc.Id
像这样?:
select VoucherId,
max(case when IsDebit = 1 then AccountID end) DebitID,
max(case when IsDebit = 1 then am.Name end) DebitName,
max(case when IsDebit = 0 then AccountID end) CreditID,
max(case when IsDebit = 0 then am.Name end) CreditName,
Amount
from VoucherDetails vd
join AccountsMaster am on am.Id = AccountID
group by VoucherId, Amount
order by VoucherId
如果您进行复式输入,这将起作用。也就是说,对于每个借方条目,还应该有一个贷方条目。
select DrVoucherId as VoucherId,DebitName,DebitId,CreditName,CreditId,Amount from (
select VoucherId as DrVoucherId,AccountName as DebitName ,AccountId as DebitId from VoucherDetails where IsDebit=1
) a
left join
(select VoucherId as CrVoucherId,AccountName as CreditName ,AccountId as CreditId,Amount from VoucherDetails where IsDebit=0) b on a.DrVoucherId=b.CrVoucherId
我无法找到与帐户相关的报告 我需要在一行中获得信用账户和借记账户..金额
我的table是这样的
Id VoucherId AccountId Amount AccountName IsDebit
1 1 26 100 Sales 0
2 1 10 100 Cash 1
3 2 26 200 Sales 0
4 2 10 200 Cash 1
5 3 10 150 Cash 0
6 3 20 150 Expense A 1
7 4 10 240 Cash 0
8 4 21 240 Expense B 1
我需要得到这样的结果
VoucherId DebitName DebitID CreditName CreditID 金额<br>
1 现金 10 销售额 26 100
2 现金 10 销售额 26 200
3 支出 a 10 现金 26 150
4 费用 b 10 现金 26 240
我试过了
select vc.Id,
am.Name,vc.AccountName,vd.[Description],
case when vd.isdebit=1 then vd.amount else 0 end as C6,
case when vd.isdebit=1 then vd.AccountId else 0 end as A,
case when vd.isdebit=0 then vd.amount else 0 end as C7,
case when vd.isdebit=0 then vd.AccountId else 0 end as B
from VoucherDetails vd
inner join Voucher Vc on vc.Id=vd.VoucherId and vc.IsDeleted=0
inner join AccountsMaster Am on am.Id=vd.AccountId
和许多其他查询,但没有得到上述结果
请帮助..提前致谢
您需要聚合:
select vc.Id,
sum(case when vd.isdebit = 1 then vd.amount else 0 end) as C6,
sum(case when vd.isdebit = 1 then vd.AccountId else 0 end) as A,
sum(case when vd.isdebit = 0 then vd.amount else 0 end) as C7,
sum(case when vd.isdebit = 0 then vd.AccountId else 0 end) as B
from VoucherDetails vd join
Voucher Vc
on vc.Id = vd.VoucherId and vc.IsDeleted = 0 join
AccountsMaster Am
on am.Id = vd.AccountId
group by vc.Id
像这样?:
select VoucherId,
max(case when IsDebit = 1 then AccountID end) DebitID,
max(case when IsDebit = 1 then am.Name end) DebitName,
max(case when IsDebit = 0 then AccountID end) CreditID,
max(case when IsDebit = 0 then am.Name end) CreditName,
Amount
from VoucherDetails vd
join AccountsMaster am on am.Id = AccountID
group by VoucherId, Amount
order by VoucherId
如果您进行复式输入,这将起作用。也就是说,对于每个借方条目,还应该有一个贷方条目。
select DrVoucherId as VoucherId,DebitName,DebitId,CreditName,CreditId,Amount from (
select VoucherId as DrVoucherId,AccountName as DebitName ,AccountId as DebitId from VoucherDetails where IsDebit=1
) a
left join
(select VoucherId as CrVoucherId,AccountName as CreditName ,AccountId as CreditId,Amount from VoucherDetails where IsDebit=0) b on a.DrVoucherId=b.CrVoucherId