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  | 
                             
                            
                                    | 
                                         emailuser 
                                        Yak Posting Veteran 
                                         
                                        
                                        74 Posts  | 
                                        
                                        
                                            
                                            
                                             Posted - 2014-06-18 : 06:18:18
                                            
  | 
                                             
                                            
                                            | Hi everyone,  Any help on this tricky problem would be greatly appreciated , we have a table called enablerscollector  that has data in the format below  Type(char100),status(char100), CreateDate(char100),CloseDate(char100)Obs           Approved         2014-06-17 16:35:27 2014-06-17 00:00:00Obs           Approved         2014-06-15 10:00:00 2014-06-16 00:00:00Kaiz          Approved         2014-06-15 11:00:00 2014-06-16 00:00:00Obs           Recorded         2014-06-15 10:00:00 2014-06-15 00:00:00Obs           For Approval     2014-06-12 11:00:00 0001-01-01 00:00:00I want to calculate the average number of calendar days that a Type 'Obs' has been open for  , on a rolling 12months Point to note is that if something is still open it shows closedate as 0001-01-01 00:00:00If something is still open beyond 12months it should still be added to the average until it is closed Hope this is not too difficult for all you genius`s out there :)  any any help is greatly greatly appreciated as always  | 
                                             
                                         
                                     | 
                             
       
                            
                       
                          
                            
                                    | 
                                     sz1 
                                    Aged Yak Warrior 
                                     
                                    
                                    555 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-06-18 : 07:45:40
                                          
  | 
                                         
                                        
                                          | Try this:With ObsAs(Select DateDiff(d, t.[CreateDate], t.[CloseDate]) as DaysOpenFrom MyTable tWhere t.[Type] = 'Obs'And t.[ClosedDate] Is NullOr t.[ClosedDate] = ''Or t.[ClosedDate] = '0001-01-01 00:00:00'And t.[CreateDate] between '2014-01-01 00:00:00' and '2014-12-31 23:59:59' --change dates here)SelectAVG(DaysOpen) From Obs**********************************************************************Or simply:Declare @StartDate as DateTime = '2014-01-01 00:00:00' -- change date hereDeclare @EndDate as DateTime = '2014-12-31 23:59:59' -- change date hereSelect Avg(DateDiff(d, t.[CreateDate], t.[CloseDate])) as AVGDaysOpenFrom MyTable tWhere t.[Type] =' Obs'And t.[ClosedDate] Is NullOR t.[ClosedDate] = ''Or t.[ClosedDate] = '0001-01-01 00:00:00'And  Convert(Date,i.[CreateDate]) = @StartDateAnd  Convert(Date,i.[ClosedDate]) = @EndDateGo********************************************************************Using CASE to breakdown each monthDeclare @StartDate as DateTime = '2014-01-01 00:00:00' -- change date hereDeclare @EndDate as DateTime = '2014-12-31 23:59:59' -- change date hereDeclare @Month as int = Month(t.[CreateDate])Select January = CaseWhen  t.[Type] =' Obs'And t.[ClosedDate] Is NullOr t.[ClosedDate] = ''Or t.[ClosedDate] = '0001-01-01 00:00:00'And @Month = 1Then Avg(DateDiff(d, t.[CreateDate], t.[CloseDate]))End,February = CaseWhen  t.[Type] =' Obs'And t.[ClosedDate] Is NullOr t.[ClosedDate] = ''Or t.[ClosedDate] = '0001-01-01 00:00:00'And @Month = 2Then Avg(DateDiff(d, t.[CreateDate], t.[CloseDate]))End,March = CaseWhen  t.[Type] =' Obs'And t.[ClosedDate] Is NullOr t.[ClosedDate] = ''Or t.[ClosedDate] = '0001-01-01 00:00:00'And @Month = 3Then Avg(DateDiff(d, t.[CreateDate], t.[CloseDate]))EndFrom MyTable tWe are the creators of our own reality!  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                            
                                | 
                                    
                                      
                                     
                                    
                                 | 
                             
                         
                     | 
                 
             
         |   
     
     
            
              
	     |  
		
			
     
          
		 |