title: Is it possible to simplify this SQL?
body: <p>Is it possible to simplify this SQL or is it good?</p>
  
  <pre><code>select 
    c.codigo, 
    c.nome,  
    coalesce(sum(a.vlcompra), 0) as total_client,
    sum(coalesce(sum(a.vlcompra), 0)) over(order by coalesce(sum(a.vlcompra), 0) desc) running_total,
    (select sum(vlcompra) from acumterc    where  tpterc = 2  and tpoper = 2  and ano = 2018 ) as total,
    coalesce(sum(a.vlcompra), 0) * 100 / (select sum(vlcompra) from acumterc    where  tpterc = 2  and 
    tpoper = 2  and ano = 2018 ) as perc1,
    sum(coalesce(sum(a.vlcompra), 0)) over(order by coalesce(sum(a.vlcompra), 0) desc) * 100 / (select 
    sum(vlcompra) from acumterc    where  tpterc = 2  and tpoper = 2  and ano = 2018 ) as perc2
  from clientes c
  left join acumterc a on a.tpterc = 2 
    and a.tpoper = 2 
    and a.terceiro = c.codigo
    and a.ano = 2018
  group by c.codigo, c.nome
  </code></pre>
  
  <p>Sample data:</p>
  
  <pre><code>codigo nome        vlcompra      ano           tpoper  tpterc
  --     --------    ------------  ------------- ------ ------ 
  45     nome1       40            2018           2       2       
  45     nome1       60            2018           2       2    
  23     nome2       100           2018           2       2    
  23     nome2       100           2018           2       2   
  23     nome2       100           2018           2       2   
  12     nome3       500           2018           2       2    
  12     nome3       100           2018           2       2  
  12     nome3       100           2019           2       2    
  </code></pre>
  
  <p>I get the desired result:</p>
  
  <pre><code>codigo nome        total_client  running_total total  perc1  perc2
  --     --------    ------------  ------------- ------ ------ ------
  45     nome1       100           100           1000   10%    10%   
  23     nome2       300           400           1000   30%    40%
  12     nome3       600           1000          1000   60%    100%
  </code></pre>
  
  <p>Where: <code>total = sum(total_client)</code> -> auxiliary column for the perc1 and perc2 calculations</p>
  
  <pre><code>   perc1 = porcentage of total_client over total
     perc2 = porcentage of running_total over total
  </code></pre>