Please start any new threads on our new 
    site at https://forums.sqlteam.com.  We've got lots of great SQL Server
    experts to answer whatever question you can come up with. 
    
        
            
                
                    
                        
                            
                                | Author | 
                                
                                 Topic  | 
                             
                            
                                    | 
                                         medotnet 
                                        Starting Member 
                                         
                                        
                                        14 Posts  | 
                                        
                                        
                                            
                                            
                                             Posted - 2007-08-21 : 04:10:58
                                            
  | 
                                             
                                            
                                            | Hi all,how can I apply this into SQL stmt:SELECT a,b, SUM(c)-SUM(d) AS result FROM A# GROUP BY a,b//now insert the result into another table similar to A#IF ( result > 0 )INSERT INTO B# VALUES ( a , b , 0 , result )ELSEINSERT INTO B# VALUES ( a , b , result , 0 )I did it by code (C++ and ADO) but the performance is bad, and I don't feel good using code instead of SQL (it is a SIN).thanks, | 
                                             
                                         
                                     | 
                             
       
                            
                       
                          
                            
                                    | 
                                     madhivanan 
                                    Premature Yak Congratulator 
                                     
                                    
                                    22864 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2007-08-21 : 04:47:21
                                          
  | 
                                         
                                        
                                          | INSERT INTO B#(columns)SELECT a,b, case when (coalesce(SUM(c),0)-coalesce(SUM(d),0))>0 then (coalesce(SUM(c),0)-coalesce(SUM(d),0))else 0 end  FROM A# GROUP BY a,bMadhivananFailing to plan is Planning to fail  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     medotnet 
                                    Starting Member 
                                     
                                    
                                    14 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2007-08-21 : 09:36:46
                                          
  | 
                                         
                                        
                                          | Well thank you Guru,I appreciate your help  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     madhivanan 
                                    Premature Yak Congratulator 
                                     
                                    
                                    22864 Posts  | 
                                    
                                      
                                     | 
                                   
                            
                            
                                | 
                                    
                                      
                                     
                                    
                                 | 
                             
                         
                     | 
                 
             
         |   
     
     
            
              
	     |  
		
			
     
          
		 |