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  | 
                             
                            
                                    | 
                                         skylimitsql 
                                        Starting Member 
                                         
                                        
                                        8 Posts  | 
                                        
                                        
                                            
                                            
                                             Posted - 2014-08-10 : 06:28:15
                                            
  | 
                                             
                                            
                                            | Hi,Partially I have been able get the result but not desired result, please help me with this.I have these two tabledeclare @T1 Table(id1 int, Name Varchar(90))insert into @T1select 1,	'Steve' union allselect 2,	'Joe' union allselect 3,	'Bob' union allselect 4,	'Julie' union allselect 5,	'Harry' union allselect 6,	'Fred' union allselect 7,	'Jill'declare @T2 Table(score int, id2 int)insert into @T2select 300,	4 union allselect 120,	6 union allselect 12,	3 union allselect 140,	6 union allselect 543,	1 union allselect 455,	2 union allselect 311,	3 union allselect 555,	6 union allselect 234,	5 A SQL query to show all the names in the T1 table that do not have cumulative scores of at least 450 (note that Jill should be included in such a list as she has taken no tests at all and therefore not scored!)So far I can fire this query select Name, TotalScore from @T1 t1  left join (select id2, SUM(score)TotalScore from @T2 group by id2 --having SUM(score) < 450)d on t1.id1 = d.id2 and TotalScore < 450ResultName	TotalScoreSteve	NULLJoe	NULLBob	323Julie	300Harry	234Fred	NULLJill	NULLbut the correct result should have beenBob	323Julie	300Harry	234Jill	NULLPlease help me to write this query | 
                                             
                                         
                                     | 
                             
       
                            
                       
                          
                            
                                    | 
                                     SwePeso 
                                    Patron Saint of Lost Yaks 
                                     
                                    
                                    30421 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-08-10 : 08:00:29
                                          
  | 
                                         
                                        
                                          [code]-- SwePesoSELECT		t1.name,		SUM(t2.score)FROM		@t1 AS t1LEFT JOIN	@t2 AS t2 ON t2.id2 = t1.id1GROUP BY	t1.name,		t1.id1HAVING		SUM(ISNULL(t2.score, 0)) < 450;[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     gbritton 
                                    Master Smack Fu Yak Hacker 
                                     
                                    
                                    2780 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-08-10 : 08:00:49
                                          
  | 
                                         
                                        
                                          | Why do you want Jill but not steve?  Both have null sums.  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     SwePeso 
                                    Patron Saint of Lost Yaks 
                                     
                                    
                                    30421 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-08-10 : 08:53:02
                                          
  | 
                                         
                                        
                                          gbritton, check the sample data. OP expected output is flawed.Steve has 543 for score. Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     skylimitsql 
                                    Starting Member 
                                     
                                    
                                    8 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-08-10 : 08:57:58
                                          
  | 
                                         
                                        
                                          | Many Thanks SwePeso. This is exactly what I wanted. Appreciate that.Regards  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     gbritton 
                                    Master Smack Fu Yak Hacker 
                                     
                                    
                                    2780 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-08-10 : 09:18:05
                                          
  | 
                                         
                                        
                                          quote: Originally posted by skylimitsql Many Thanks SwePeso. This is exactly what I wanted. Appreciate that.Regards
  Well the subquery returns no rows for steve since his score is too high.Then the left join gives him null correctly.  So my question still applies.  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                            
                                | 
                                    
                                      
                                     
                                    
                                 | 
                             
                         
                     | 
                 
             
         |   
     
     
            
              
	     |  
		
			
     
          
		 |