Haven´t been able to solve this: : Can't solve this and just haven´t been able to wonder whats going wrong:


    SELECT	  CODE
    		, NAME
    		, BRANCH
    		, SALESP
       		, CASE 
    			WHEN SUM(S1) = 0 THEN NULL
    			ELSE SUM(S1) END as S1
    		, CASE 
    			WHEN SUM(V1) = 0 THEN NULL
    			ELSE SUM(V1) END as V1
    		, CASE 
    			WHEN SUM(CHANGE) = 0 THEN NULL
    			ELSE SUM(CHANGE) END as CHANGE
    
    FROM SAESQL7_E03.[dbo].[p1]
      
    WHERE 	S1 <> 0 
    		OR CHANGE <> 0
    		
    
    GROUP BY
    		  CODE
    		, NAME
    		, BRANCH
    		, SALESP

Resulting in:

    code  name    branch   salesp   s1   v1   change
    1     Max     A        S        20   10    -5
    2     Ben     A                 10    3     5
    2     Ben     B                 20    5    10

The problem is in the 3rd column which is always wrong if a customer is in more than one branch.

(Max´s example when customer only in one branch, always right; Ben´s example case with two or more branches, first branch always right, following branches always wrong)

The table of origin has the correct data being:

     
       code  name    branch   salesp    V1   V2   change
        1     Max     A        S         5   10    -5
        2     Ben     A                  8    3     5
        2     Ben     B                 10    5    10

CHANGE field comes from:

       SUM (V1-V2) AS CHANGE



Help will be greatly appreciated!!!
	