我有以下表格
1. Ledger
LedgerId LedgerName
1 Cash acoount
2 My Account
2. Transaction
TransactionId EntryDate
1 26/5/2014
3. DebitCredit
DebitCreditId TransactionId LedgerId IsDebit Amount
1 1 1 0 50.00
2 1 2 1 50.00
我的输出应该是
Date Debit Ledger Name Credit Ledger Name Amount
26/5/2014 My Account Cash Account 50
如果IsDebit为1,则相应的分类帐名称将显示为借方分类帐名称,否则将显示为贷方分类帐名称
请提供此
的查询帮帮我 感谢
答案 0 :(得分:0)
这可以使用JOINs:
来实现SELECT Date = t.EntryDate,
DebitLedgerName = dl.LedgerName,
CreditLedgerName = cl.LedgerName,
d.Amount
FROM [Transaction] AS t
INNER JOIN DebitCredit AS d
ON d.TransactionID = t.TransactionID
AND d.IsDebit = 1
INNER JOIN Ledger AS dl
ON dl.LedgerID = d.LedgerID
INNER JOIN DebitCredit AS c
ON c.TransactionID = t.TransactionID
AND c.IsDebit = 0
INNER JOIN Ledger AS cl
ON cl.LedgerID = c.LedgerID;
<强> Example on SQL Fiddle 强>