title: Query for finding Sum of credit and debit
body: <p>I need a SQL query to retrieve SUM in  DATE ranges.</p>
  
  <p>this is my sudo code:</p>
  
  <pre><code> SELECT da.Debit,da.Credit,
      (SELECT SUM(daa.Debit) FROM Acc.DocumentAccount daa
      INNER JOIN Acc.Document dd ON dd.Id = daa.DocumentId
       WHERE dd.Date BETWEEN '2018-03-21' AND '2018-06-21' AND daa.Id=da.id) SumDebit
       FROM Acc.DocumentAccount da 
      INNER JOIN Acc.Document d ON d.Id = da.DocumentId
      WHERE d.Date BETWEEN '2018-06-21' AND '2019-03-21'
  </code></pre>
  
  <p>consider following sample data:</p>
  
  <pre><code>CREATE TABLE Document(
      Id INT,
      [Date] date
  );
  
  
  INSERT INTO Document VALUES (1,'2018-01-01');
  INSERT INTO Document VALUES (2,'2018-01-02');
  INSERT INTO Document VALUES (3,'2018-07-01');
  INSERT INTO Document VALUES (4,'2018-07-02');
  CREATE TABLE DocumentAccount(
      DocumentId INT,
      Debit INT,
      Credit INT
      );
  
  INSERT INTO DocumentAccount VALUES (1,100,200);
  INSERT INTO DocumentAccount VALUES (2,150,300);
  INSERT INTO DocumentAccount VALUES (3,150,300);
  INSERT INTO DocumentAccount VALUES (4,160,310);
  INSERT INTO DocumentAccount VALUES (4,170,320);
  
  CREATE TABLE DocumentAccountDetail
  (
      DocumentAccountId INT,
      DetailId INT 
  )
  </code></pre>
  
  <p>I need this hard coded data:</p>
  
  <pre><code>select * from 
  (values (250, 500,0,0,480,0,0,0,930,0)) x(RemainDebit, RemainCredit,Q1Debit,Q2Debit,Q3Debit,Q4Debit,Q1Credit,Q2Credit,Q3Credit,Q4Credit)
  </code></pre>
  
  <p><code>RemainDebit</code> is sum of <code>debit</code> between '2018-03-21' AND '2018-06-21'</p>
  
  <p><code>RemainCredit</code> is sum of <code>credit</code> between <code>'2018-03-21'</code> AND <code>'2018-06-21'</code> </p>
  
  <p><code>Q1Debit</code> is sum of <code>debit</code> between <code>'2018-01-01'</code> AND <code>'2018-03-30'</code> in this case because my filter range is BETWEEN <code>'2018-06-21'</code> AND <code>'2019-03-21'</code> Q1Debit is 0   </p>
  
  <p><code>Q2Debit</code> is sum of <code>debit</code> between <code>'2018-04-01'</code> AND <code>'2018-06-30'</code> in this case because my filter range is BETWEEN <code>'2018-06-21'</code> AND <code>'2019-03-21'</code> Q1Debit is 0  </p>
  
  <p><code>Q3Debit</code> is sum of <code>debit</code> between <code>'2018-06-01'</code> AND <code>'2018-09-30'</code> in this case because my filter range is BETWEEN <code>'2018-06-21'</code> AND <code>'2019-03-21'</code> and we have 3 document in 2018-07 then  Q1Debit is 150+160+170=480</p>
  
  <p>etc... </p>