- sql server loop through id's to get dataset
- select gl.AcctName [Account Name], gl.AcctNumber [Account Number]
- , isnull(Debit.FacilityID,Credit.FacilityID) as FacilityID
- , isnull(Debit.Amount,0) as Debit , isnull(Credit.Amount,0) as Credit
- from GL_Account gl WITH (NOLOCK)
- left join (
- select crd.CR_Acct, cr.FacilityID, abs(sum(crd.Amount)) as Amount
- from CashRec cr WITH (NOLOCK)
- inner join CashRecDetail crd WITH (NOLOCK) on crd.CashRecID=cr.CashRecID
- where cr.TransactionDate between '2011-12-01' and '2011-12-31'
- and cr.FacilityID=12
- and crd.Amount<0
- group by crd.CR_Acct, cr.FacilityID
- ) Debit on Debit.CR_Acct=gl.AcctID
- left join (
- select crd.CR_Acct, cr.FacilityID, sum(crd.Amount) as Amount
- from CashRec cr WITH (NOLOCK)
- inner join CashRecDetail crd WITH (NOLOCK) on crd.CashRecID=cr.CashRecID
- where cr.TransactionDate between '2011-12-01' and '2011-12-31'
- and cr.FacilityID=12
- and crd.Amount>=0
- group by cr.FacilityID, crd.CR_Acct
- ) Credit on Credit.CR_Acct=gl.AcctID
- where Credit.Amount is not null or Debit.Amount is not null
- order by gl.AcctNumber
- Current Output:
- Account Name Account Number FacilityID Debit Credit
- Account A 102100 12 0.00 239073.21
- Account B 102120 12 0.00 15766.92
- Account C 102200 12 42939.39 237820.82
- Desired Output:
- Account Name Account Number FacilityID Debit Credit
- Account A 102100 12 0.00 239073.21
- Account B 102120 12 0.00 15766.92
- Account C 102200 12 42939.39 237820.82
- Account A 102100 13 10.00 0.00
- Account B 102120 13 20.00 1234.92
- Account C 102200 13 22222.39 237820.82
- etc...