title: Group by then Sort (SQL Server)
body: <p>Following the previous <a href="https://stackoverflow.com/questions/47958656/order-by-x-then-order-by-y-column-in-sql-server?noredirect=1#comment82887224_47958656">question</a> </p>
  
  <p><strong>I have this query:</strong></p>
  
  <pre><code>SELECT  Acc.DocTLItem.TLRef ,
          Acc.DocTLItem.Debit AS deb,
          Acc.DocTLItem.Credit AS cred,
          info.MiladiToShamsi(Acc.DocTLItem.StartDocDate) Date,
          Acc.TL.TLCode ,
          Acc.DocTLItem.DocTLHeaderRef ,
          Acc.DocTLHeader.Num
  FROM    Acc.DocTLItem
          INNER JOIN Acc.TL ON Acc.DocTLItem.TLRef = Acc.TL.Id
          INNER JOIN Acc.DocTLHeader ON Acc.DocTLItem.DocTLHeaderRef = Acc.DocTLHeader.Id
          ORDER BY ( CASE WHEN debit &gt; 0 THEN 0  ELSE 1 END ) ,
          Acc.TL.TLCode ,
          debit
  </code></pre>
  
  <p><strong>Result:</strong></p>
  
  <pre><code> TLRef  deb cred        Date    TLCode  DocTLHeaderRef  Num
      --------------------------------------------------------------------------
      44   1  0       1396/09/12  111     16           2
      44   1  0       1396/09/21  111     18           4
      28   13 0       1396/09/11  982     15           1
      28   10 0       1396/09/19  982     17           3
      44   0  10      1396/09/19  111     17           3
      44   0  1       1396/09/21  111     18           4
      44   0  9       1396/09/11  111     15           1
      44   0  1       1396/09/12  111     16           2
  </code></pre>
  
  <p>How can I Group by Date then sort by Date?</p>
  
  <p>I need to generate a result set like this that debt comes first and then ordered by TLCode column after all group by date.</p>
  
  <p><strong>Expected result:</strong></p>
  
  <pre><code>  TLRef deb cred    Date    TLCode  DocTLHeaderRef  Num
      --------------------------------------------------------------------------------
      44  1    0  1396/09/12  111         16           2
      28  13   0  1396/09/11  982         15           1
      28  10   0  1396/09/19  982         17           3
      44  0    9  1396/09/11  111         15           1
      44  0    1  1396/09/12  111         16           2
      44  0    10 1396/09/19  111         17           3
      Sum 24   20             
  
      44  1   0   1396/09/21  111         18           4
      44  0   1   1396/09/21  111         18           4
      Sum 1   1   
  </code></pre>