Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 3rd, 2012  |  syntax: None  |  size: 1.88 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. sql server loop through id's to get dataset
  2. select gl.AcctName [Account Name], gl.AcctNumber [Account Number]
  3. , isnull(Debit.FacilityID,Credit.FacilityID) as FacilityID
  4. , isnull(Debit.Amount,0) as Debit , isnull(Credit.Amount,0) as Credit
  5. from GL_Account gl WITH (NOLOCK)
  6. left join (
  7.     select crd.CR_Acct, cr.FacilityID, abs(sum(crd.Amount)) as Amount
  8.     from CashRec cr WITH (NOLOCK)
  9.     inner join CashRecDetail crd WITH (NOLOCK) on crd.CashRecID=cr.CashRecID
  10.     where cr.TransactionDate between '2011-12-01' and '2011-12-31'
  11.     and cr.FacilityID=12
  12.     and crd.Amount<0
  13.     group by crd.CR_Acct, cr.FacilityID
  14.     ) Debit on Debit.CR_Acct=gl.AcctID
  15. left join (
  16.     select crd.CR_Acct, cr.FacilityID, sum(crd.Amount) as Amount
  17.     from CashRec cr WITH (NOLOCK)
  18.     inner join CashRecDetail crd WITH (NOLOCK) on crd.CashRecID=cr.CashRecID
  19.     where cr.TransactionDate between '2011-12-01' and '2011-12-31'
  20.     and cr.FacilityID=12
  21.     and crd.Amount>=0
  22.     group by cr.FacilityID, crd.CR_Acct
  23. ) Credit on Credit.CR_Acct=gl.AcctID
  24. where Credit.Amount is not null or Debit.Amount is not null
  25. order by gl.AcctNumber
  26.  
  27. Current Output:
  28. Account Name    Account Number  FacilityID  Debit           Credit
  29. Account A            102100         12          0.00        239073.21
  30. Account B            102120         12          0.00        15766.92
  31. Account C            102200         12          42939.39    237820.82
  32.  
  33. Desired Output:
  34. Account Name    Account Number  FacilityID  Debit           Credit
  35. Account A            102100         12          0.00        239073.21
  36. Account B            102120         12          0.00        15766.92
  37. Account C            102200         12          42939.39    237820.82
  38. Account A            102100         13          10.00       0.00
  39. Account B            102120         13          20.00       1234.92
  40. Account C            102200         13          22222.39    237820.82
  41. etc...