title: T-sql; adding positive and negative values with SUM()
body: <p>I haven´t done any T-SQL before and were wondering how could I change the script below to differentiate between positive and negative values. At the moment it seems to just to add the two together and giving out wrong values.</p>
  
  <p>I would like to add an IF clause that s.summa would follow rules:
   e.g </p>
  
  <blockquote>
    <p>-1 + -1 = -2</p>
    
    <p>1 + -1 = 0</p>
    
    <p>1 + 1 = 2</p>
  </blockquote>
  
  <p>If I am correct now it is being dealt with "etumerkki", so that if etumerkki variable is 0 s.sum is converted to negative and if etumerkki variable has not been set it remains normal.</p>
  
  <pre><code>SET @stmt = CONCAT( 'INSERT INTO tbl_1 (rivi,rivi_id,isanta_rivi,taso,lihavointi,tili_rivi,otsikko,selite,summa)
                          SELECT  t.rivi , t.rivi_id , t.isanta_rivi , t.taso , t.lihavointi , t.rivi_tyyppi , t.otsikko , t.selite , 
                                  CASE etumerkki
                                      WHEN 0 THEN -1 * IFNULL(SUM(s.summa),0.00) 
                                      ELSE IFNULL(SUM(s.summa),0.00) 
                                  END AS summa
                          FROM tase_mem t
                                  LEFT JOIN kuutio_paakirja s ON s.tili = t.tili
                          WHERE   ' , nyk_summa_ehto , ' 
                                  AND s.kpaikka_id = ' , kpaikka_id , ' AND s.projekti_id = ' , projekti_id , ' ' , isanta_ehto , '
                          GROUP BY t.rivi , t.rivi_id , t.taso , t.lihavointi , t.rivi_tyyppi , t.etumerkki , t.otsikko , t.selite;');
      PREPARE stmt FROM @stmt;
      EXECUTE stmt;
  </code></pre>
  
  <p>Thanks in advance</p>